Skip to content

Commit

Permalink
Fix mitchellhGH-340: Decoding array of slices causes panic
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Oct 30, 2023
1 parent bf980b3 commit 58a4c3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mapstructure.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}
}

0 comments on commit 58a4c3b

Please sign in to comment.