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 GH-340: Decoding array of slices causes panic #343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion mapstructure.go
Expand Up @@ -1163,7 +1163,7 @@ func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value)

valArray := val

if valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields {
if valArray.Comparable() && valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields {
// Check input type
if dataValKind != reflect.Array && dataValKind != reflect.Slice {
if d.config.WeaklyTypedInput {
Expand Down
16 changes: 16 additions & 0 deletions mapstructure_bugs_test.go
Expand Up @@ -625,3 +625,19 @@ func TestMapOmitEmptyWithEmptyFieldnameInTag(t *testing.T) {
t.Fatalf("fail: %#v", m)
}
}

// GH-340: Decoding array of slices causes panic
type HasNonComparableType struct {
NonComparableType [2][]byte
}

func TestDecode_nonComparableType(t *testing.T) {
decodeTo := &HasNonComparableType{}
expected := [2][]byte{{1, 2}, {3, 4, 5}}
if err := Decode(map[string]interface{}{"NonComparableType": expected}, &decodeTo); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, decodeTo.NonComparableType) {
t.Fatalf("fail: %#v", decodeTo.NonComparableType)
}
}