Skip to content

Commit

Permalink
don't use compact floats in msgpack
Browse files Browse the repository at this point in the history
There is a bug in the compact floats implementation, which could cause
some integer values stored in a float to encode as an incorrect value.
The result of the expression used to check for compact integer encoding
is `float64(int64(n)) == n`, which is undefined when conversion is
not exact.
  • Loading branch information
jbardin authored and apparentlymart committed Jan 30, 2024
1 parent a0315a5 commit 1e9442d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cty/msgpack/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func Marshal(val cty.Value, ty cty.Type) ([]byte, error) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
enc.UseCompactInts(true)
enc.UseCompactFloats(true)

// UseCompactFloats can fail on some platforms due to undefined behavior of
// float conversions
enc.UseCompactFloats(false)

err := marshal(val, ty, path, enc)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions cty/msgpack/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ func TestRoundTrip(t *testing.T) {
bigNumberVal,
cty.Number,
},
{
cty.MustParseNumberVal("9223372036854775807"),
cty.Number,
},
{
cty.MustParseNumberVal("9223372036854775808"),
cty.Number,
},
{
cty.MustParseNumberVal("9223372036854775809"),
cty.Number,
},
{
awkwardFractionVal,
cty.Number,
Expand Down

0 comments on commit 1e9442d

Please sign in to comment.