Skip to content

Commit

Permalink
internal/fwschemadata: Prevent panics in `ValueSemanticEqualityListEl…
Browse files Browse the repository at this point in the history
…ements` and `ValueSemanticEqualitySetElements` methods when the length of `newValueElements` is greater than `priorValueElements` (#772)

* Prevent panic in `ValueSemanticEqualityListElements` and `ValueSemanticEqualitySetElements` when the number of new value elements is greater than prior value elements.

* Add Changie entry
  • Loading branch information
SBGoods authored Jun 14, 2023
1 parent 22f77e2 commit 8c25df4
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230614-142449.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'types/basetypes: Prevented panic with `ListTypableWithSemanticEquals` and `SetTypableWithSemanticEquals`
when proposed new element count was greater than prior element count'
time: 2023-06-14T14:24:49.334761-04:00
custom:
Issue: "772"
2 changes: 1 addition & 1 deletion internal/fwschemadata/value_semantic_equality_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func ValueSemanticEqualityListElements(ctx context.Context, req ValueSemanticEqu
// Ensure new value always contains all of proposed new value
newValueElements[idx] = proposedNewValueElement

if idx > len(priorValueElements) {
if idx >= len(priorValueElements) {
continue
}

Expand Down
76 changes: 76 additions & 0 deletions internal/fwschemadata/value_semantic_equality_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
Expand Down Expand Up @@ -333,6 +334,81 @@ func TestValueSemanticEqualityList(t *testing.T) {
),
},
},
"ListValue-ListValue-StringValuableWithSemanticEquals-NewValueElementsGreaterThanPriorValueElements": {
request: fwschemadata.ValueSemanticEqualityRequest{
Path: path.Root("test"),
PriorValue: types.ListValueMust(
types.ListType{
ElemType: testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
},
[]attr.Value{
types.ListValueMust(
testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
[]attr.Value{
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("prior"),
SemanticEquals: true,
},
},
),
},
),
ProposedNewValue: types.ListValueMust(
types.ListType{
ElemType: testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
},
[]attr.Value{
types.ListValueMust(
testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
[]attr.Value{
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("new1"),
SemanticEquals: true,
},
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("new2"),
SemanticEquals: true,
},
},
),
},
),
},
expected: &fwschemadata.ValueSemanticEqualityResponse{
NewValue: types.ListValueMust(
types.ListType{
ElemType: testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
},
[]attr.Value{
types.ListValueMust(
testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
[]attr.Value{
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("prior"),
SemanticEquals: true,
},
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("new2"),
SemanticEquals: true,
},
},
),
},
),
},
},
"ListValue-ListValue-StringValuableWithSemanticEquals-diagnostics": {
request: fwschemadata.ValueSemanticEqualityRequest{
Path: path.Root("test"),
Expand Down
2 changes: 1 addition & 1 deletion internal/fwschemadata/value_semantic_equality_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func ValueSemanticEqualitySetElements(ctx context.Context, req ValueSemanticEqua
// Ensure new value always contains all of proposed new value
newValueElements[idx] = proposedNewValueElement

if idx > len(priorValueElements) {
if idx >= len(priorValueElements) {
continue
}

Expand Down
76 changes: 76 additions & 0 deletions internal/fwschemadata/value_semantic_equality_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
Expand Down Expand Up @@ -333,6 +334,81 @@ func TestValueSemanticEqualitySet(t *testing.T) {
),
},
},
"SetValue-SetValue-StringValuableWithSemanticEquals-NewValueElementsGreaterThanPriorValueElements": {
request: fwschemadata.ValueSemanticEqualityRequest{
Path: path.Root("test"),
PriorValue: types.SetValueMust(
types.SetType{
ElemType: testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
},
[]attr.Value{
types.SetValueMust(
testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
[]attr.Value{
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("prior"),
SemanticEquals: true,
},
},
),
},
),
ProposedNewValue: types.SetValueMust(
types.SetType{
ElemType: testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
},
[]attr.Value{
types.SetValueMust(
testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
[]attr.Value{
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("new1"),
SemanticEquals: true,
},
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("new2"),
SemanticEquals: true,
},
},
),
},
),
},
expected: &fwschemadata.ValueSemanticEqualityResponse{
NewValue: types.SetValueMust(
types.SetType{
ElemType: testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
},
[]attr.Value{
types.SetValueMust(
testtypes.StringTypeWithSemanticEquals{
SemanticEquals: true,
},
[]attr.Value{
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("prior"),
SemanticEquals: true,
},
testtypes.StringValueWithSemanticEquals{
StringValue: types.StringValue("new2"),
SemanticEquals: true,
},
},
),
},
),
},
},
"SetValue-SetValue-StringValuableWithSemanticEquals-diagnostics": {
request: fwschemadata.ValueSemanticEqualityRequest{
Path: path.Root("test"),
Expand Down

0 comments on commit 8c25df4

Please sign in to comment.