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

panic on marshaling values obtained from unexported field #463

Open
nirekin opened this issue May 10, 2019 · 7 comments · May be fixed by #1059
Open

panic on marshaling values obtained from unexported field #463

nirekin opened this issue May 10, 2019 · 7 comments · May be fixed by #1059

Comments

@nirekin
Copy link

nirekin commented May 10, 2019

Output

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
	panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 1 [running]:
<installer/vendor/gopkg.in/yaml%2ev2.handleErr(0xc4204e2340)
	/go/src/installer/vendor/gopkg.in/yaml.v2/yaml.go:249 +0x99
panic(0x91e080, 0xa68950)
	/usr/local/go/src/runtime/panic.go:502 +0x229
reflect.valueInterface(0x9b4be0, 0xc42000e650, 0x56, 0x91e001, 0x91e001, 0x16)
	/usr/local/go/src/reflect/value.go:959 +0x1c1
reflect.Value.Interface(0x9b4be0, 0xc42000e650, 0x56, 0x91e000, 0xc420394b60)
	/usr/local/go/src/reflect/value.go:948 +0x44
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x9b4be0, 0xc42000e650, 0x56)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:106 +0x74
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv.func1()
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:229 +0x24b
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc420461600, 0x0, 0x0, 0xc4204e1158)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:259 +0x14e
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv(0xc420461600, 0x0, 0x0, 0x9d5940, 0xc42000e650, 0x19)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:216 +0xf7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x9d5940, 0xc42000e650, 0x19)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:163 +0x6c7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x9a90c0, 0xc420114120, 0x194)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:150 +0x632
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv.func1()
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:229 +0x24b
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc420461600, 0x0, 0x0, 0xc4204e1670)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:259 +0x14e
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv(0xc420461600, 0x0, 0x0, 0x983de0, 0xc420114120, 0x199)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:216 +0xf7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x983de0, 0xc420114120, 0x199)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:163 +0x6c7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv.func1()
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:229 +0x24b
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc420461600, 0x0, 0x0, 0xc4204e1a88)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:259 +0x14e
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv(0xc420461600, 0x0, 0x0, 0x9ab9e0, 0xc420114120, 0x199)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:216 +0xf7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x9ab9e0, 0xc420114120, 0x199)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:163 +0x6c7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x970160, 0xc420162580, 0x96)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:157 +0x53b
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv.func1()
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:229 +0x24b
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc420461600, 0x0, 0x0, 0xc4204e1fa0)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:259 +0x14e
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).structv(0xc420461600, 0x0, 0x0, 0x9df060, 0xc420162380, 0x99)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:216 +0xf7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshal(0xc420461600, 0x0, 0x0, 0x9df060, 0xc420162380, 0x99)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:163 +0x6c7
installer/vendor/gopkg.in/yaml%2ev2.(*encoder).marshalDoc(0xc420461600, 0x0, 0x0, 0x9df060, 0xc420162380, 0x99)
	/go/src/installer/vendor/gopkg.in/yaml.v2/encode.go:96 +0x238
installer/vendor/gopkg.in/yaml%2ev2.Marshal(0x9df060, 0xc420162380, 0x0, 0x0, 0x0, 0x0, 0x0)
	/go/src/installer/vendor/gopkg.in/yaml.v2/yaml.go:203 +0xe0

Analysis :

yaml/encode.go

Lines 98 to 103 in 7b8349a

func (e *encoder) marshal(tag string, in reflect.Value) {
if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() {
e.nilv()
return
}
iface := in.Interface()

Solution :
in.CanInterface() should be called before Interface() in order to be sure it won't panic

func (e *encoder) marshal(tag string, in reflect.Value) {
	if !in.CanInterface() || !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() {
		e.nilv()
		return
	}
	iface := in.Interface()
       ...
}
 
@nirekin nirekin changed the title panic on marshalling values obtained from unexported field panic on marshaling values obtained from unexported field May 10, 2019
@niemeyer
Copy link
Contributor

Thanks for the report. Do you have some minimal sample code reproducing the bug?

@johnSchnake
Copy link

https://play.golang.org/p/WxT-pwqhNx4 will reproduce the bug. Having an embedded type which is unexported.

@denisvmedia
Copy link

This problem seems to be still actual. Even in v3. The solution by @nirekin seems to be working. Are there any plans to include it in the upcoming releases? CC @niemeyer

@roffe
Copy link

roffe commented Oct 8, 2022

Still getting it on V3 in 2022 when trying to marshal a ECS service response from aws-sdk-go-v2

This is the struct it's panicing on marshalling https://github.com/aws/aws-sdk-go-v2/blob/main/service/ecs/types/types.go#L2826-L3007

Can confirm fix by @nirekin works

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
        panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 1 [running]:
gopkg.in/yaml%2ev2.handleErr(0xc000523998)
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/yaml.go:249 +0x6d
panic({0x15fdb20, 0x1aa5d00})
        /usr/local/go/src/runtime/panic.go:884 +0x212
reflect.valueInterface({0x169d0e0?, 0xc000880220?, 0x0?}, 0x20?)
        /usr/local/go/src/reflect/value.go:1489 +0xd8
reflect.Value.Interface(...)
        /usr/local/go/src/reflect/value.go:1478
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc000136888?, {0x0, 0x0}, {0x169d0e0?, 0xc000880220?, 0x4?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:103 +0xb7
gopkg.in/yaml%2ev2.(*encoder).structv.func1()
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:226 +0x4da
gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc0000f6dc0, {0x0?, 0x0}, 0xc000522f10)
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:256 +0x147
gopkg.in/yaml%2ev2.(*encoder).structv(0xc0000f6dc0, {0x0, 0x0}, {0x1740f00?, 0xc000880210?, 0xc0000f6dc0?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:213 +0xef
gopkg.in/yaml%2ev2.(*encoder).marshal(0x15c1400?, {0x0, 0x0}, {0x1740f00?, 0xc000880210?, 0x8?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:160 +0x9f9
gopkg.in/yaml%2ev2.(*encoder).slicev(0xc0000f6dc0, {0x0?, 0x0}, {0x15c1400?, 0xc000602b60?, 0x12?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:272 +0x1df
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc000136738?, {0x0, 0x0}, {0x15c1400?, 0xc000602b60?, 0x20?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:166 +0x907
gopkg.in/yaml%2ev2.(*encoder).structv.func1()
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:226 +0x4da
gopkg.in/yaml%2ev2.(*encoder).mappingv(0xc0000f6dc0, {0x0?, 0x0}, 0xc0005234f8)
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:256 +0x147
gopkg.in/yaml%2ev2.(*encoder).structv(0xc0000f6dc0, {0x0, 0x0}, {0x17fd5e0?, 0xc000602b60?, 0xb?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:213 +0xef
gopkg.in/yaml%2ev2.(*encoder).marshal(0x163d960?, {0x0, 0x0}, {0x17fd5e0?, 0xc000602b60?, 0x0?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:160 +0x9f9
gopkg.in/yaml%2ev2.(*encoder).marshal(0xc0000f6dc0?, {0x0, 0x0}, {0x163d960?, 0xc000602b60?, 0x109?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:154 +0x855
gopkg.in/yaml%2ev2.(*encoder).marshalDoc(0xc0000f6dc0, {0x0, 0x0}, {0x163d960?, 0xc000602b60?, 0x183b0ef?})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/encode.go:93 +0x125
gopkg.in/yaml%2ev2.Marshal({0x163d960?, 0xc000602b60})
        /home/roffe/go/pkg/mod/gopkg.in/[email protected]/yaml.go:203 +0x379
github.com/tjek/tjek-cli/cmd.printService(0xc000602b60?)
        /home/roffe/go/src/github.com/tjek/tjek-cli/cmd/getService.go:95 +0x8c
github.com/tjek/tjek-cli/cmd.glob..func22(0x25a5ea0?, {0xc0002f7ec0, 0x1, 0x3?})
        /home/roffe/go/src/github.com/tjek/tjek-cli/cmd/scaleRoot.go:53 +0x318
github.com/spf13/cobra.(*Command).execute(0x25a5ea0, {0xc0002f7e30, 0x3, 0x3})
        /home/roffe/go/pkg/mod/github.com/spf13/[email protected]/command.go:856 +0x663
github.com/spf13/cobra.(*Command).ExecuteC(0x25a54a0)
        /home/roffe/go/pkg/mod/github.com/spf13/[email protected]/command.go:960 +0x39d
github.com/spf13/cobra.(*Command).Execute(...)
        /home/roffe/go/pkg/mod/github.com/spf13/[email protected]/command.go:897
github.com/tjek/tjek-cli/cmd.Execute()
        /home/roffe/go/src/github.com/tjek/tjek-cli/cmd/root.go:53 +0x25
main.main()
        /home/roffe/go/src/github.com/tjek/tjek-cli/main.go:11 +0x2a
exit status 2

@roffe
Copy link

roffe commented Oct 8, 2022

When can we expect a buggfix to be released @niemeyer

@saddit
Copy link

saddit commented Mar 29, 2023

Why not fix it?

@jjrodenburg jjrodenburg linked a pull request Dec 5, 2024 that will close this issue
@jjrodenburg
Copy link

I encountered this error today so raised another PR and added 2 simple unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants