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

Conversation

bendbennett
Copy link
Contributor

@bendbennett bendbennett commented May 11, 2022

Overview

Issue 1

A lossy conversion between int64 and uint64 can arise because of the use of float64(), which has a precision of 53.

This issue arises in the following locations:

Taking the form:

return NewValue(Number, big.NewFloat(float64(rv))), nil

Issue 2

Another potential lossy operation can arise from the usage of big.Float.Set.

This issue arises in the following location:

This is a consequence of the following:

v, ok := val.value.(*big.Float)
if !ok {
	return fmt.Errorf("can't unmarshal %s into %T, expected *big.Float", val.Type(), dst)
}
target.Set(v)

Because v uses the default precision of 53 of big.Float

An additional consequence of using target.Set(v) is that the tests within value_msgpack_test.go would not detect this form of rounding.

Fixes

  • new(big.Float).SetInt64() or new(big.Float).SetUint64() is used as appropriate in value_msgpack.go.
  • big.Float.Copy instead of big.Float.Set is used within value.go.
  • Tests within value_msgpack_test.go have been updated to check for rounding.

Related Issues

Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🍳 😘

Copy link
Member

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just needs a CHANGELOG entry added, e.g. in .changelog/190.txt

```release-note:bug
tftypes: Prevented loss of number precision with integers between 54 and 64 bits
```

@bflad bflad added the bug Something isn't working label May 11, 2022
@bflad bflad added this to the v0.10.0 milestone May 11, 2022
@bendbennett bendbennett merged commit 4f3425d into main May 12, 2022
@bendbennett bendbennett deleted the bendbennett/issues-189 branch May 12, 2022 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid plan arising from integer rounding
3 participants