expvar: don't crash if map value set to nil

Fixes #52719

Change-Id: Ib032193d00664090c47ae92e7d59674ec2d0165a
Reviewed-on: https://go-review.googlesource.com/c/go/+/408677
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Ian Lance Taylor 2022-05-28 07:51:56 -07:00 committed by Gopher Robot
parent 32510eea74
commit ef808ae1d4
2 changed files with 31 additions and 2 deletions

View file

@ -118,7 +118,12 @@ func (v *Map) String() string {
if !first {
fmt.Fprintf(&b, ", ")
}
fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value)
fmt.Fprintf(&b, "%q: ", kv.Key)
if kv.Value != nil {
fmt.Fprintf(&b, "%v", kv.Value)
} else {
fmt.Fprint(&b, "null")
}
first = false
})
fmt.Fprintf(&b, "}")
@ -224,7 +229,8 @@ func (v *Map) Do(f func(KeyValue)) {
defer v.keysMu.RUnlock()
for _, k := range v.keys {
i, _ := v.m.Load(k)
f(KeyValue{k, i.(Var)})
val, _ := i.(Var)
f(KeyValue{k, val})
}
}

View file

@ -261,6 +261,29 @@ func TestMapCounter(t *testing.T) {
}
}
func TestMapNil(t *testing.T) {
RemoveAll()
const key = "key"
m := NewMap("issue527719")
m.Set(key, nil)
s := m.String()
var j any
if err := json.Unmarshal([]byte(s), &j); err != nil {
t.Fatalf("m.String() == %q isn't valid JSON: %v", s, err)
}
m2, ok := j.(map[string]any)
if !ok {
t.Fatalf("m.String() produced %T, wanted a map", j)
}
v, ok := m2[key]
if !ok {
t.Fatalf("missing %q in %v", key, m2)
}
if v != nil {
t.Fatalf("m[%q] = %v, want nil", key, v)
}
}
func BenchmarkMapSet(b *testing.B) {
m := new(Map).Init()