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 failure on line 1166 in mapstructure.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

valArray.Comparable undefined (type reflect.Value has no field or method Comparable)

Check failure on line 1166 in mapstructure.go

View workflow job for this annotation

GitHub Actions / Test (1.18)

valArray.Comparable undefined (type reflect.Value has no field or method Comparable)

Check failure on line 1166 in mapstructure.go

View workflow job for this annotation

GitHub Actions / Test (1.19)

valArray.Comparable undefined (type reflect.Value has no field or method Comparable)
// 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.