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/xml: marshaller panics in reflect.Value.CanInterface #43595

Closed
d-neal opened this issue Jan 8, 2021 · 3 comments
Closed

encoding/xml: marshaller panics in reflect.Value.CanInterface #43595

d-neal opened this issue Jan 8, 2021 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@d-neal
Copy link

d-neal commented Jan 8, 2021

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

$ go version
go version go1.15.6 darwin/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="/Users/d-neal/Library/Caches/go-build"
GOENV="/Users/d-neal/Library/Application Support/go/env"
GOEXE=""
GOFLAGS="-mod=vendor"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/d-neal/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/d-neal/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.6/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/d-neal/repos/goroute/go.mod"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0d/6gm66r353ynfl6d90m65tlmr0000gn/T/go-build360898696=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/1txISuTviPw

The problem is not reproducible with golang 1.14
It is also not reproducible if I just remove the XML tags from Foo/Bar attributes

What did you expect to see?

<Bar b="b"></Bar>

What did you see instead?

panic: reflect: call of reflect.Value.CanInterface on zero Value

goroutine 1 [running]:
reflect.Value.CanInterface(...)
	/usr/local/go-faketime/src/reflect/value.go:1005
encoding/xml.(*printer).marshalAttr(0xc00010e000, 0xc000068dd0, 0x0, 0x0, 0x4bec8b, 0x1, 0x0, 0x0, 0x0, 0xc000068d78, ...)
	/usr/local/go-faketime/src/encoding/xml/marshal.go:551 +0x1465
encoding/xml.(*printer).marshalValue(0xc00010e000, 0x4c0aa0, 0xc00000c0e0, 0x16, 0x0, 0x0, 0x90, 0x4cb820)
	/usr/local/go-faketime/src/encoding/xml/marshal.go:517 +0x5f0
encoding/xml.(*Encoder).Encode(0xc00010e000, 0x4c0aa0, 0xc00000c0e0, 0xc00007e000, 0x20)
	/usr/local/go-faketime/src/encoding/xml/marshal.go:162 +0xb6
encoding/xml.MarshalIndent(0x4c0aa0, 0xc00000c0e0, 0x0, 0x0, 0x4e0092, 0x2, 0xc000032778, 0xc000068f78, 0x4060e5, 0xc00005e058, ...)
	/usr/local/go-faketime/src/encoding/xml/marshal.go:129 +0x159
main.main()
	/tmp/sandbox825762527/prog.go:22 +0x7b
@mvdan mvdan added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 8, 2021
@bradleypeabody
Copy link

I ran into this same issue. Interestingly enough, if you remove the pointer on the embedded Foo field, it works:

type Bar struct {
	Foo
	...

https://play.golang.org/p/e561Lk2yaM8

It also works if Foo (pointer) is populated with an empty instance (non-nil pointer):

	bar := &Bar{
		Foo: &Foo{},
		...

https://play.golang.org/p/Ar2OZfBapRw

With just a quick scan I didn't see anything in the docs for encoding/xml that mentions embedded structs either way. But I think it's reasonable to assume that if it works as field Foo then *Foo should work as well and have the same behavior as if Foo had a zero value. Most likely somewhere in the internals it is reading the value of each field and expecting to get a zero value for a struct and try to recursively traverse it, and instead gets a nil pointer and chokes on that. (That's just a guess, but a reasonable one. The error reflect.Value.CanInterface on zero Value is exactly what would be encountered after calling https://golang.org/pkg/reflect/#Value.Elem on a nil pointer and then trying to use the resulting reflect.Value.)

@seankhliao seankhliao changed the title XML marshaller panics in reflect.Value.CanInterface encoding/xml: marshaller panics in reflect.Value.CanInterface Jun 18, 2021
@hopehook
Copy link
Member

hopehook commented Mar 6, 2022

I think this issue is the same as issue #50164, and should be closed by CL https://go.dev/cl/385514.

/cc @seankhliao

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
@hopehook
Copy link
Member

hopehook commented Sep 1, 2022

Already fixed.

@hopehook hopehook closed this as completed Sep 1, 2022
@golang golang locked and limited conversation to collaborators Sep 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants