Skip to content

Commit

Permalink
basetypes: fix equality for values with nil elementType (#961)
Browse files Browse the repository at this point in the history
* basetypes: fix equality for MapValue with nil elementType

A `MapValue` with a nil `elementType` is an invalid state, and therefore cannot verify equality with another value. This change to the `MapValue.Equal` method now returns `false` if either the receiver or argument have a nil `elementType`.

* basetypes: fix equality for ListValue with nil elementType

* basetypes: fix equality for SetValue with nil elementType

* chore: changelog
  • Loading branch information
jar-b committed Apr 17, 2024
1 parent d36ac87 commit de32b2c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240319-085739.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'types/basetypes: Prevented panic in the `MapValue` types `Equal` method when
the receiver has a nil `elementType`'
time: 2024-03-19T08:57:39.837509-04:00
custom:
Issue: "961"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240319-085908.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'types/basetypes: Prevented panic in the `ListValue` types `Equal` method when
the receiver has a nil `elementType`'
time: 2024-03-19T08:59:08.576502-04:00
custom:
Issue: "961"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240319-085928.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'types/basetypes: Prevented panic in the `SetValue` types `Equal` method when
the receiver has a nil `elementType`'
time: 2024-03-19T08:59:28.116063-04:00
custom:
Issue: "961"
5 changes: 5 additions & 0 deletions types/basetypes/list_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ func (l ListValue) Equal(o attr.Value) bool {
return false
}

// A list with no elementType is an invalid state
if l.elementType == nil || other.elementType == nil {
return false
}

if !l.elementType.Equal(other.elementType) {
return false
}
Expand Down
15 changes: 15 additions & 0 deletions types/basetypes/list_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,21 @@ func TestListValueEqual(t *testing.T) {
input: nil,
expected: false,
},
"zero-null": {
receiver: ListValue{},
input: NewListNull(StringType{}),
expected: false,
},
"zero-zero": {
receiver: ListValue{},
input: ListValue{},
expected: false,
},
"null-zero": {
receiver: NewListNull(StringType{}),
input: ListValue{},
expected: false,
},
}
for name, test := range tests {
name, test := name, test
Expand Down
5 changes: 5 additions & 0 deletions types/basetypes/map_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func (m MapValue) Equal(o attr.Value) bool {
return false
}

// A map with no elementType is an invalid state
if m.elementType == nil || other.elementType == nil {
return false
}

if !m.elementType.Equal(other.elementType) {
return false
}
Expand Down
15 changes: 15 additions & 0 deletions types/basetypes/map_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ func TestMapValueEqual(t *testing.T) {
input: nil,
expected: false,
},
"zero-null": {
receiver: MapValue{},
input: NewMapNull(StringType{}),
expected: false,
},
"zero-zero": {
receiver: MapValue{},
input: MapValue{},
expected: false,
},
"null-zero": {
receiver: NewMapNull(StringType{}),
input: MapValue{},
expected: false,
},
}
for name, test := range tests {
name, test := name, test
Expand Down
5 changes: 5 additions & 0 deletions types/basetypes/set_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ func (s SetValue) Equal(o attr.Value) bool {
return false
}

// A set with no elementType is an invalid state
if s.elementType == nil || other.elementType == nil {
return false
}

if !s.elementType.Equal(other.elementType) {
return false
}
Expand Down
15 changes: 15 additions & 0 deletions types/basetypes/set_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,21 @@ func TestSetValueEqual(t *testing.T) {
input: nil,
expected: false,
},
"zero-null": {
receiver: SetValue{},
input: NewSetNull(StringType{}),
expected: false,
},
"zero-zero": {
receiver: SetValue{},
input: SetValue{},
expected: false,
},
"null-zero": {
receiver: NewSetNull(StringType{}),
input: SetValue{},
expected: false,
},
}
for name, test := range tests {
name, test := name, test
Expand Down

0 comments on commit de32b2c

Please sign in to comment.