From de32b2cb370d531433d21e0908f3128017255fe6 Mon Sep 17 00:00:00 2001 From: Jared Baker Date: Wed, 17 Apr 2024 11:58:06 -0400 Subject: [PATCH] basetypes: fix equality for values with nil elementType (#961) * 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 --- .../unreleased/BUG FIXES-20240319-085739.yaml | 6 ++++++ .../unreleased/BUG FIXES-20240319-085908.yaml | 6 ++++++ .../unreleased/BUG FIXES-20240319-085928.yaml | 6 ++++++ types/basetypes/list_value.go | 5 +++++ types/basetypes/list_value_test.go | 15 +++++++++++++++ types/basetypes/map_value.go | 5 +++++ types/basetypes/map_value_test.go | 15 +++++++++++++++ types/basetypes/set_value.go | 5 +++++ types/basetypes/set_value_test.go | 15 +++++++++++++++ 9 files changed, 78 insertions(+) create mode 100644 .changes/unreleased/BUG FIXES-20240319-085739.yaml create mode 100644 .changes/unreleased/BUG FIXES-20240319-085908.yaml create mode 100644 .changes/unreleased/BUG FIXES-20240319-085928.yaml diff --git a/.changes/unreleased/BUG FIXES-20240319-085739.yaml b/.changes/unreleased/BUG FIXES-20240319-085739.yaml new file mode 100644 index 000000000..86e99cab1 --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20240319-085739.yaml @@ -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" diff --git a/.changes/unreleased/BUG FIXES-20240319-085908.yaml b/.changes/unreleased/BUG FIXES-20240319-085908.yaml new file mode 100644 index 000000000..6f7824894 --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20240319-085908.yaml @@ -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" diff --git a/.changes/unreleased/BUG FIXES-20240319-085928.yaml b/.changes/unreleased/BUG FIXES-20240319-085928.yaml new file mode 100644 index 000000000..a45876280 --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20240319-085928.yaml @@ -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" diff --git a/types/basetypes/list_value.go b/types/basetypes/list_value.go index f89cbb73f..d0ec03027 100644 --- a/types/basetypes/list_value.go +++ b/types/basetypes/list_value.go @@ -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 } diff --git a/types/basetypes/list_value_test.go b/types/basetypes/list_value_test.go index cdb706b6b..e63eb8088 100644 --- a/types/basetypes/list_value_test.go +++ b/types/basetypes/list_value_test.go @@ -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 diff --git a/types/basetypes/map_value.go b/types/basetypes/map_value.go index 56a64361c..7d819eca9 100644 --- a/types/basetypes/map_value.go +++ b/types/basetypes/map_value.go @@ -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 } diff --git a/types/basetypes/map_value_test.go b/types/basetypes/map_value_test.go index 4561b4a6e..0debecd9f 100644 --- a/types/basetypes/map_value_test.go +++ b/types/basetypes/map_value_test.go @@ -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 diff --git a/types/basetypes/set_value.go b/types/basetypes/set_value.go index 9beb2da3d..2064e8fb2 100644 --- a/types/basetypes/set_value.go +++ b/types/basetypes/set_value.go @@ -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 } diff --git a/types/basetypes/set_value_test.go b/types/basetypes/set_value_test.go index e5d63f67e..260382d0b 100644 --- a/types/basetypes/set_value_test.go +++ b/types/basetypes/set_value_test.go @@ -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