Skip to content

Commit 4f3425d

Browse files
authored
Invalid plan arising from integer rounding (#190)
* Removing lossy conversion to big.Float (#189) * Adding additional tests for MinInt64 and SmallestNonZeroFloat64 (#189) * Adding changelog (#189) * Update CHANGELOG for v0.9.1
1 parent 8734625 commit 4f3425d

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

.changelog/190.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
tftypes: Prevented loss of number precision with integers between 54 and 64 bits
3+
```

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.9.1 (May 12, 2022)
2+
3+
BUG FIXES:
4+
* tftypes: Prevented loss of number precision with integers between 54 and 64 bits ([#190](https://github.com/hashicorp/terraform-plugin-go/issues/190))
5+
16
# 0.9.0 (April 13, 2022)
27

38
NOTES:

tftypes/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (val Value) As(dst interface{}) error {
440440
if !ok {
441441
return fmt.Errorf("can't unmarshal %s into %T, expected *big.Float", val.Type(), dst)
442442
}
443-
target.Set(v)
443+
target.Copy(v)
444444
return nil
445445
case **big.Float:
446446
if val.IsNull() {

tftypes/value_msgpack.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ func msgpackUnmarshal(dec *msgpack.Decoder, typ Type, path *AttributePath) (Valu
7474
if err != nil {
7575
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
7676
}
77-
return NewValue(Number, big.NewFloat(float64(rv))), nil
77+
return NewValue(Number, new(big.Float).SetInt64(rv)), nil
7878
}
7979
switch peek {
8080
case msgpackCodes.Int8, msgpackCodes.Int16, msgpackCodes.Int32, msgpackCodes.Int64:
8181
rv, err := dec.DecodeInt64()
8282
if err != nil {
8383
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
8484
}
85-
return NewValue(Number, big.NewFloat(float64(rv))), nil
85+
return NewValue(Number, new(big.Float).SetInt64(rv)), nil
8686
case msgpackCodes.Uint8, msgpackCodes.Uint16, msgpackCodes.Uint32, msgpackCodes.Uint64:
8787
rv, err := dec.DecodeUint64()
8888
if err != nil {
8989
return Value{}, path.NewErrorf("couldn't decode number as uint64: %w", err)
9090
}
91-
return NewValue(Number, big.NewFloat(float64(rv))), nil
91+
return NewValue(Number, new(big.Float).SetUint64(rv)), nil
9292
case msgpackCodes.Float, msgpackCodes.Double:
9393
rv, err := dec.DecodeFloat64()
9494
if err != nil {

tftypes/value_msgpack_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,36 @@ func TestValueFromMsgPack(t *testing.T) {
7070
value: NewValue(Number, big.NewFloat(1)),
7171
typ: Number,
7272
},
73+
"int64-positive-number": {
74+
hex: "cf7fffffffffffffff",
75+
value: NewValue(Number, new(big.Float).SetInt64(math.MaxInt64)),
76+
typ: Number,
77+
},
78+
"int64-negative-number": {
79+
hex: "d38000000000000000",
80+
value: NewValue(Number, new(big.Float).SetInt64(math.MinInt64)),
81+
typ: Number,
82+
},
83+
"uint64-number": {
84+
hex: "b43138343436373434303733373039353531363135",
85+
value: NewValue(Number, new(big.Float).SetUint64(math.MaxUint64)),
86+
typ: Number,
87+
},
7388
"float-number": {
7489
hex: "cb3ff8000000000000",
7590
value: NewValue(Number, big.NewFloat(1.5)),
7691
typ: Number,
7792
},
93+
"float64-positive-number": {
94+
hex: "cb7fefffffffffffff",
95+
value: NewValue(Number, new(big.Float).SetFloat64(math.MaxFloat64)),
96+
typ: Number,
97+
},
98+
"float64-negative-number": {
99+
hex: "cb0000000000000001",
100+
value: NewValue(Number, new(big.Float).SetFloat64(math.SmallestNonzeroFloat64)),
101+
typ: Number,
102+
},
78103
"big-number": {
79104
hex: "d96439393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939",
80105
value: NewValue(Number, bigNumber),

0 commit comments

Comments
 (0)