From 836a94d705c40bdbb1e944d091ff3adb29adfe49 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 5 Oct 2021 16:20:19 -0400 Subject: [PATCH] types: Prevent panic with uninitialized Number Value (#200) Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/89 Reference: https://github.com/hashicorp/terraform-plugin-go/pull/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, }, ``` --- .changelog/200.txt | 3 +++ types/number.go | 3 +++ types/number_test.go | 4 ++++ 3 files changed, 10 insertions(+) create mode 100644 .changelog/200.txt diff --git a/.changelog/200.txt b/.changelog/200.txt new file mode 100644 index 000000000..b95a09621 --- /dev/null +++ b/.changelog/200.txt @@ -0,0 +1,3 @@ +```release-note:bug +types: Prevent panic with uninitialized `Number` `Value` +``` diff --git a/types/number.go b/types/number.go index f342c1da9..c47e1d24e 100644 --- a/types/number.go +++ b/types/number.go @@ -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 } diff --git a/types/number_test.go b/types/number_test.go index f30b1617f..5dac7ed4e 100644 --- a/types/number_test.go +++ b/types/number_test.go @@ -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,