Skip to content

Commit 1e9442d

Browse files
jbardinapparentlymart
authored andcommitted
don't use compact floats in msgpack
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.
1 parent a0315a5 commit 1e9442d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

cty/msgpack/marshal.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ func Marshal(val cty.Value, ty cty.Type) ([]byte, error) {
3232
var buf bytes.Buffer
3333
enc := msgpack.NewEncoder(&buf)
3434
enc.UseCompactInts(true)
35-
enc.UseCompactFloats(true)
35+
36+
// UseCompactFloats can fail on some platforms due to undefined behavior of
37+
// float conversions
38+
enc.UseCompactFloats(false)
3639

3740
err := marshal(val, ty, path, enc)
3841
if err != nil {

cty/msgpack/roundtrip_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func TestRoundTrip(t *testing.T) {
8484
bigNumberVal,
8585
cty.Number,
8686
},
87+
{
88+
cty.MustParseNumberVal("9223372036854775807"),
89+
cty.Number,
90+
},
91+
{
92+
cty.MustParseNumberVal("9223372036854775808"),
93+
cty.Number,
94+
},
95+
{
96+
cty.MustParseNumberVal("9223372036854775809"),
97+
cty.Number,
98+
},
8799
{
88100
awkwardFractionVal,
89101
cty.Number,

0 commit comments

Comments
 (0)