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 when Decode's input is array and output is a slice #264

Closed
suzuki-shunsuke opened this issue Dec 22, 2021 · 2 comments · Fixed by #265
Closed

panic when Decode's input is array and output is a slice #264

suzuki-shunsuke opened this issue Dec 22, 2021 · 2 comments · Fixed by #265

Comments

@suzuki-shunsuke
Copy link
Contributor

I found the problem when I investigated the issue of Terraform AWS Provider.

ref. hashicorp/terraform-provider-aws#22312 (comment)

Panic occurs when an array is passed as Decode's input.

How to reproduce

You can reproduce the problem by passing an array as Decode's input.

https://go.dev/play/p/BMxes5mbumY

panic: reflect: call of reflect.Value.IsNil on array Value

goroutine 1 [running]:
reflect.Value.IsNil(...)
	/usr/local/go-faketime/src/reflect/value.go:1427
github.com/mitchellh/mapstructure.(*Decoder).decodeSlice(0xc000120018, {0x0, 0x0}, {0x4b4aa0, 0xc00010a4b0}, {0x4b2fa0, 0xc00011a030, 0x203000})
	/tmp/gopath2282395412/pkg/mod/github.com/mitchellh/mapstructure@v1.4.3/mapstructure.go:1091 +0xbfe
github.com/mitchellh/mapstructure.(*Decoder).decode(0xc000120018, {0x0, 0xc000126001}, {0x4b4aa0, 0xc00010a4b0}, {0x4b2fa0, 0xc00011a030, 0x48})
	/tmp/gopath2282395412/pkg/mod/github.com/mitchellh/mapstructure@v1.4.3/mapstructure.go:469 +0x3ff
github.com/mitchellh/mapstructure.(*Decoder).Decode(0xc000120018, {0x4b4aa0, 0xc00010a4b0})
	/tmp/gopath2282395412/pkg/mod/github.com/mitchellh/mapstructure@v1.4.3/mapstructure.go:398 +0xd8
github.com/mitchellh/mapstructure.Decode({0x4b4aa0, 0xc00010a4b0}, {0x4b0be0, 0xc00011a030})
	/tmp/gopath2282395412/pkg/mod/github.com/mitchellh/mapstructure@v1.4.3/mapstructure.go:302 +0x91
main.core()
	/tmp/sandbox2579048427/prog.go:19 +0x65
main.main()
	/tmp/sandbox2579048427/prog.go:11 +0x19

Program exited.

And you can fix the problem by changing the input type from array to slice.

https://go.dev/play/p/xrfz5X8RJis

@suzuki-shunsuke
Copy link
Contributor Author

if dataVal.IsNil() {

https://pkg.go.dev/reflect#Value.IsNil

The argument must be a chan, func, interface, map, pointer, or slice value; if it is not, IsNil panics.

@suzuki-shunsuke suzuki-shunsuke changed the title panic when an array is passed as Decode's input panic when Decode's input is array and output is a slice Dec 23, 2021
@suzuki-shunsuke
Copy link
Contributor Author

I have sent a pull request to fix the issue.
#265

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.

1 participant