Skip to content
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: embedded *struct implementing UnmarshalJSON is nil and panics on Unmarshal #36716

Closed
sabey opened this issue Jan 23, 2020 · 1 comment

Comments

@sabey
Copy link

sabey commented Jan 23, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.6 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jackson/.cache/go-build"
GOENV="/home/jackson/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jackson/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build498302187=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/DZoQ9ro3jv7
my parent struct inherits UnmarshalJSON(data []byte) error from the embedded struct

https://github.com/golang/go/blob/master/src/encoding/json/decode.go#L492
in the indirect() function
if u, ok := v.Interface().(Unmarshaler); ok { is true, but since the embedded struct is not allocated it will cause a nil pointer exception

encoding/json.(*decodeState).object(0xc0002ccd10, 0x5e9bc0, 0xc000132f98, 0x16, 0xc0002ccd38, 0xc0003abe7b)
/usr/local/go/src/encoding/json/decode.go:667 +0x220b

What did you expect to see?

&{&{}}

What did you see instead?

&{<nil>}

Unmarshal should allocate the embedded struct so that we can call UnmarshalJSON()
this would only be relevant if there were only 1 embedded struct which implements UnmarshalJSON

@toothrot
Copy link
Contributor

Thanks for the thoughtful issue report. This is a duplicate of #30148. Feel free to discuss there if you have more to contribute.

@golang golang locked and limited conversation to collaborators Jan 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants