1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:24:41 -07:00

don't crash printing a nil map

R=rsc
DELTA=19  (18 added, 0 deleted, 1 changed)
OCL=32656
CL=32670
This commit is contained in:
Rob Pike 2009-08-03 13:34:20 -07:00
parent 9dc22b6d6f
commit cb9c973829
2 changed files with 19 additions and 1 deletions

View File

@ -270,3 +270,17 @@ func TestMapPrinter(t *testing.T) {
presentInMap(Sprintf("%v", m1), a, t);
presentInMap(Sprint(m1), a, t);
}
func TestEmptyMap(t *testing.T) {
const emptyMapStr = "map[]";
var m map[string]int;
s := Sprint(m);
if s != emptyMapStr {
t.Errorf("nil map printed as %q not %q", s, emptyMapStr);
}
m = make(map[string]int);
s = Sprint(m);
if s != emptyMapStr {
t.Errorf("empty map printed as %q not %q", s, emptyMapStr);
}
}

View File

@ -1008,8 +1008,12 @@ func (v *MapValue) Len() int {
func (v *MapValue) Keys() []Value {
tk := v.Type().(*MapType).Key();
m := *(**byte)(v.addr);
mlen := int32(0);
if m != nil {
mlen = maplen(m)
}
it := mapiterinit(m);
a := make([]Value, maplen(m));
a := make([]Value, mlen);
var i int;
for i = 0; i < len(a); i++ {
k := MakeZero(tk);