Skip to content

Commit

Permalink
types: Prevent panic with uninitialized Number Value (#200)
Browse files Browse the repository at this point in the history
Reference: #89
Reference: hashicorp/terraform-plugin-go#114

This change will ensure that if the `Number` `Value` pointer is `nil`, that the returned value is an untyped `nil`, rather than a `*big.Float` typed `nil`.

The upstream `(tftypes.Value).IsNull()` method can only detect untyped `nil` for values. A related fix for `tftypes.NewValue()` to convert a `*big.Float` `nil` value into an untyped `nil` has also been merged upstream to fix this in depth.

Previously, this new test case would pass:

```go
		"value-nil": {
			input:       Number{Value: nil},
			expectation: (*big.Float)(nil),
		},
```

Now it appropriately expects:

```go
		"value-nil": {
			input:       Number{Value: nil},
			expectation: nil,
		},
```
  • Loading branch information
bflad authored Oct 5, 2021
1 parent 14a2ac6 commit 836a94d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/200.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
types: Prevent panic with uninitialized `Number` `Value`
```
3 changes: 3 additions & 0 deletions types/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (n Number) ToTerraformValue(_ context.Context) (interface{}, error) {
if n.Unknown {
return tftypes.UnknownValue, nil
}
if n.Value == nil {
return nil, nil
}
return n.Value, nil
}

Expand Down
4 changes: 4 additions & 0 deletions types/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func TestNumberToTerraformValue(t *testing.T) {
input: Number{Value: big.NewFloat(123)},
expectation: big.NewFloat(123),
},
"value-nil": {
input: Number{Value: nil},
expectation: nil,
},
"unknown": {
input: Number{Unknown: true},
expectation: tftypes.UnknownValue,
Expand Down

0 comments on commit 836a94d

Please sign in to comment.