From 85f3ca74882703042bcc2f17353eda192d649920 Mon Sep 17 00:00:00 2001 From: William Poussier Date: Sun, 1 Sep 2019 15:38:31 +0000 Subject: encoding/json: fix panic for nil instances of TextMarshaler in map keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds a a check in the encodeWithString.resolve method to ensure that a reflect.Value with kind Ptr is not nil before the type assertion to TextMarshaler. If the value is nil, the method returns a nil error, and the map key encodes to an empty string. Fixes #33675 Change-Id: I0a04cf690ae67006f6a9c5f8cbb4cc99d236bca8 GitHub-Last-Rev: 6c987c90846f854e21814dbfb3a073605ec8a94c GitHub-Pull-Request: golang/go#33700 Reviewed-on: https://go-review.googlesource.com/c/go/+/190697 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Daniel Martí --- src/encoding/json/encode.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/encoding/json/encode.go') diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go index 07d3098f1c..f085b5a08d 100644 --- a/src/encoding/json/encode.go +++ b/src/encoding/json/encode.go @@ -932,6 +932,9 @@ func (w *reflectWithString) resolve() error { return nil } if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok { + if w.v.Kind() == reflect.Ptr && w.v.IsNil() { + return nil + } buf, err := tm.MarshalText() w.s = string(buf) return err -- cgit v1.3