Skip to content

Commit 1df2217

Browse files
authored
Fix panic for Value.As(**big.Float). (#85)
*big.Float is a pointer type that will never really appear as anything except a pointer. So when using code like this: ```go var foo *big.Float err := value.As(&foo) ``` the target of `As` is actually a **big.Float. But our code dereferences it and calls `As` on the target of the pointer, but the target of the pointer is actually a nil pointer. This code fixes that by instantiating a new value for the pointer to point to, setting the pointer to that, and then calling `As` on the new value.
1 parent 26c9b68 commit 1df2217

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

.changelog/85.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
Fixed a panic when calling `tftypes.Value.As` and passing a pointer to an uninstantiated *big.Float.
3+
```

tftypes/value.go

+3
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ func (val Value) As(dst interface{}) error {
432432
*target = nil
433433
return nil
434434
}
435+
if *target == nil {
436+
*target = big.NewFloat(0)
437+
}
435438
return val.As(*target)
436439
case *bool:
437440
if val.IsNull() {

tftypes/value_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func TestValueAs(t *testing.T) {
9393
as: numberPointerPointer(big.NewFloat(0)),
9494
expected: numberPointerPointer(big.NewFloat(123)),
9595
},
96+
"uninstantiated-number-pointer": {
97+
in: NewValue(Number, big.NewFloat(123)),
98+
as: numberPointerPointer(nil),
99+
expected: numberPointerPointer(big.NewFloat(123)),
100+
},
96101
"number-pointer-null": {
97102
in: NewValue(Number, nil),
98103
as: numberPointerPointer(big.NewFloat(123)),

0 commit comments

Comments
 (0)