-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encoding/json: panic for nil map key types implementing TextMarshaler #33675
Comments
Could you check older Go versions, to see if this was a regression at some point? Could you also investigate if this happens with either |
The change was introduced after this proposal #12146 in this CL https://go-review.googlesource.com/c/go/+/20356/. It was released with go1.7 (see @mvdan Marshaling a type that implements the I discovered this bug while writing the testsuite for a custom JSON encoder, as part of a personal project (most of the test cases outputs are compared against the standard library), and I found no other related issues (using last version 1.12.9). /cc @augustoroman for original changes |
Fair enough, thanks. Can you send your change above as a CL, along with a test? We can continue the discussion there. Without investigating further, this seems like something to fix. |
@mvdan Sure thing. I'll send a CL. |
Change https://golang.org/cl/190697 mentions this issue: |
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 golang#33675 Change-Id: I0a04cf690ae67006f6a9c5f8cbb4cc99d236bca8 GitHub-Last-Rev: 6c987c9 GitHub-Pull-Request: golang#33700 Reviewed-on: https://go-review.googlesource.com/c/go/+/190697 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Given the following program that use a map key type implementing the
encoding.TextMarshaler
interface, when the key is nil, the code panic.https://play.golang.org/p/KcDK-mOPbqe
What did you expect to see?
Expected a
json.UnsupportedValueError
, or the key to be encoded as an empty string.What did you see instead?
Following panic stack trace.
Taken from playground output.
The
(encodeWithString).resolve
method does not check if areflect.Value
of kindPtr
is nil before doing the type assertion toencoding.TextMarshaler
with the result of the(reflect.Value).Interface
call.After adding the following check after line 869 of
encoding/json/encode.go
:I now get this output:
Am I missing something regarding
nil
keys in maps ?The text was updated successfully, but these errors were encountered: