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

fix: panic when Decode's input is array and output is a slice #265

Merged
merged 2 commits into from Apr 20, 2022
Merged

fix: panic when Decode's input is array and output is a slice #265

merged 2 commits into from Apr 20, 2022

Conversation

suzuki-shunsuke
Copy link
Contributor

@suzuki-shunsuke suzuki-shunsuke commented Dec 23, 2021

Close #264

Test

I have confirmed the issue is reproduced by 74cdd45

$ go test -race -run Decode_decodeSliceWithArray ./...
--- FAIL: TestDecode_decodeSliceWithArray (0.00s)
    --- FAIL: TestDecode_decodeSliceWithArray/input_is_array (0.00s)
panic: reflect: call of reflect.Value.IsNil on array Value [recovered]
	panic: reflect: call of reflect.Value.IsNil on array Value

goroutine 20 [running]:
testing.tRunner.func1.2({0x1237e60, 0xc0001b21b0})
	/usr/local/Cellar/go/1.17.5/libexec/src/testing/testing.go:1209 +0x36c
testing.tRunner.func1()
	/usr/local/Cellar/go/1.17.5/libexec/src/testing/testing.go:1212 +0x3b6
panic({0x1237e60, 0xc0001b21b0})
	/usr/local/Cellar/go/1.17.5/libexec/src/runtime/panic.go:1047 +0x266
reflect.Value.IsNil(...)
	/usr/local/Cellar/go/1.17.5/libexec/src/reflect/value.go:1427
github.com/mitchellh/mapstructure.(*Decoder).decodeSlice(0xc0001a4048, {0x0, 0x0}, {0x1233220, 0x13b9f68}, {0x1230140, 0xc0001b2180, 0x9ca1ed8})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:1092 +0xfd1
github.com/mitchellh/mapstructure.(*Decoder).decode(0xc0001a4048, {0x0, 0x2880008}, {0x1233220, 0x13b9f68}, {0x1230140, 0xc0001b2180, 0x10aa9bb})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:469 +0x815
github.com/mitchellh/mapstructure.(*Decoder).decodePtr(0xc0001a4048, {0x0, 0x0}, {0x1233220, 0x13b9f68}, {0xc0001e06c0, 0xc0001b2180, 0x1230140})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:1031 +0x73b
github.com/mitchellh/mapstructure.(*Decoder).decode(0xc0001a4048, {0x0, 0x8}, {0x1233220, 0x13b9f68}, {0xc0001e06c0, 0xc0001b2180, 0xc0001a62a0})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:467 +0x630
github.com/mitchellh/mapstructure.(*Decoder).decodeBasic(0x1078872, {0x0, 0x0}, {0x1233220, 0x13b9f68}, {0x1236aa0, 0xc0001e06a0, 0x12313e0})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:513 +0x63f
github.com/mitchellh/mapstructure.(*Decoder).decode(0xc0001a4048, {0x0, 0x3636000009ca2f50}, {0x1233220, 0x13b9f68}, {0x1236aa0, 0xc0001e06a0, 0xc00018e1d0})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:453 +0x67b
github.com/mitchellh/mapstructure.(*Decoder).Decode(0xc0001a4048, {0x1233220, 0x13b9f68})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:398 +0x1dd
github.com/mitchellh/mapstructure.Decode({0x1233220, 0x13b9f68}, {0x122cdc0, 0xc0001e06a0})
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure.go:302 +0x151
github.com/mitchellh/mapstructure.TestDecode_decodeSliceWithArray.func1(0xc000183a00)
	/Users/shunsuke-suzuki/repos/src/github.com/mitchellh/mapstructure/mapstructure_test.go:580 +0x87
testing.tRunner(0xc000183a00, 0xc00019ad60)
	/usr/local/Cellar/go/1.17.5/libexec/src/testing/testing.go:1259 +0x230
created by testing.(*T).Run
	/usr/local/Cellar/go/1.17.5/libexec/src/testing/testing.go:1306 +0x727
FAIL	github.com/mitchellh/mapstructure	0.246s
FAIL

And the issue is solved by ab94595

$ go test -race -run Decode_decodeSliceWithArray ./...
ok  	github.com/mitchellh/mapstructure	0.245s

@suzuki-shunsuke suzuki-shunsuke changed the title test: add a test to reproduce the bug fix: panic when Decode's input is array and output is a slice Dec 23, 2021
@@ -1088,7 +1088,7 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
}

// If the input value is nil, then don't allocate since empty != nil
if dataVal.IsNil() {
if dataValKind != reflect.Array && dataVal.IsNil() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@@ -556,6 +556,38 @@ func TestDecode_EmbeddedArray(t *testing.T) {
}
}

func TestDecode_decodeSliceWithArray(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry but I don't know how to add tests in this repository.
If this isn't good, please let me know.
I'll fix.

@mitchellh mitchellh merged commit ab94595 into mitchellh:master Apr 20, 2022
@mitchellh
Copy link
Owner

Thank you!

@suzuki-shunsuke suzuki-shunsuke deleted the fix/fix-issue-264 branch April 20, 2022 22:39
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 this pull request may close these issues.

panic when Decode's input is array and output is a slice
2 participants