Skip to content

Commit

Permalink
Invalid plan arising from integer rounding (#190)
Browse files Browse the repository at this point in the history
* Removing lossy conversion to big.Float (#189)

* Adding additional tests for MinInt64 and SmallestNonZeroFloat64 (#189)

* Adding changelog (#189)

* Update CHANGELOG for v0.9.1
  • Loading branch information
bendbennett authored May 12, 2022
1 parent 8734625 commit 4f3425d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/190.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tftypes: Prevented loss of number precision with integers between 54 and 64 bits
```
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.9.1 (May 12, 2022)

BUG FIXES:
* tftypes: Prevented loss of number precision with integers between 54 and 64 bits ([#190](https://github.com/hashicorp/terraform-plugin-go/issues/190))

# 0.9.0 (April 13, 2022)

NOTES:
Expand Down
2 changes: 1 addition & 1 deletion tftypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (val Value) As(dst interface{}) error {
if !ok {
return fmt.Errorf("can't unmarshal %s into %T, expected *big.Float", val.Type(), dst)
}
target.Set(v)
target.Copy(v)
return nil
case **big.Float:
if val.IsNull() {
Expand Down
6 changes: 3 additions & 3 deletions tftypes/value_msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ func msgpackUnmarshal(dec *msgpack.Decoder, typ Type, path *AttributePath) (Valu
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
return NewValue(Number, new(big.Float).SetInt64(rv)), nil
}
switch peek {
case msgpackCodes.Int8, msgpackCodes.Int16, msgpackCodes.Int32, msgpackCodes.Int64:
rv, err := dec.DecodeInt64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
return NewValue(Number, new(big.Float).SetInt64(rv)), nil
case msgpackCodes.Uint8, msgpackCodes.Uint16, msgpackCodes.Uint32, msgpackCodes.Uint64:
rv, err := dec.DecodeUint64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as uint64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
return NewValue(Number, new(big.Float).SetUint64(rv)), nil
case msgpackCodes.Float, msgpackCodes.Double:
rv, err := dec.DecodeFloat64()
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions tftypes/value_msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,36 @@ func TestValueFromMsgPack(t *testing.T) {
value: NewValue(Number, big.NewFloat(1)),
typ: Number,
},
"int64-positive-number": {
hex: "cf7fffffffffffffff",
value: NewValue(Number, new(big.Float).SetInt64(math.MaxInt64)),
typ: Number,
},
"int64-negative-number": {
hex: "d38000000000000000",
value: NewValue(Number, new(big.Float).SetInt64(math.MinInt64)),
typ: Number,
},
"uint64-number": {
hex: "b43138343436373434303733373039353531363135",
value: NewValue(Number, new(big.Float).SetUint64(math.MaxUint64)),
typ: Number,
},
"float-number": {
hex: "cb3ff8000000000000",
value: NewValue(Number, big.NewFloat(1.5)),
typ: Number,
},
"float64-positive-number": {
hex: "cb7fefffffffffffff",
value: NewValue(Number, new(big.Float).SetFloat64(math.MaxFloat64)),
typ: Number,
},
"float64-negative-number": {
hex: "cb0000000000000001",
value: NewValue(Number, new(big.Float).SetFloat64(math.SmallestNonzeroFloat64)),
typ: Number,
},
"big-number": {
hex: "d96439393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939",
value: NewValue(Number, bigNumber),
Expand Down

0 comments on commit 4f3425d

Please sign in to comment.