Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid plan arising from integer rounding #190

Merged
merged 4 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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