From 1e9442db3a138ce23781155142e6377d42947aac Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 30 Jan 2024 17:25:46 -0500 Subject: [PATCH] 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. --- cty/msgpack/marshal.go | 5 ++++- cty/msgpack/roundtrip_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cty/msgpack/marshal.go b/cty/msgpack/marshal.go index 27da762a..274d559b 100644 --- a/cty/msgpack/marshal.go +++ b/cty/msgpack/marshal.go @@ -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 { diff --git a/cty/msgpack/roundtrip_test.go b/cty/msgpack/roundtrip_test.go index 497919b4..5b73ea9a 100644 --- a/cty/msgpack/roundtrip_test.go +++ b/cty/msgpack/roundtrip_test.go @@ -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,