From 24f0c47fe7d6b2252cffae47281897894fb4eb08 Mon Sep 17 00:00:00 2001 From: Damian Nadales Date: Thu, 31 Aug 2023 13:52:33 +0200 Subject: [PATCH 1/3] Make `tokenTypeOf` more precise Now `tokenTypeOf` distinguishes between: - Unsigned 64 bit integers `(0 <= n < 2^64)`: `TypeUInt64` - Negative 64 bit integers `(-2^64 < n < 0 )`: `TypeNInt64` - Integers outside the ranges above : `TypeInteger` Fixes https://github.com/well-typed/cborg/issues/324 --- cborg/src/Codec/CBOR/FlatTerm.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cborg/src/Codec/CBOR/FlatTerm.hs b/cborg/src/Codec/CBOR/FlatTerm.hs index a630624..0036d5b 100644 --- a/cborg/src/Codec/CBOR/FlatTerm.hs +++ b/cborg/src/Codec/CBOR/FlatTerm.hs @@ -564,7 +564,10 @@ tokenTypeOf :: TermToken -> TokenType tokenTypeOf (TkInt n) | n >= 0 = TypeUInt | otherwise = TypeNInt -tokenTypeOf TkInteger{} = TypeInteger +tokenTypeOf (TkInteger n) -- See https://github.com/well-typed/cborg/issues/324 + | n >= 0 && n < 2^64 = TypeUInt64 + | n < 0 && n > -2^64 = TypeNInt64 + | otherwise = TypeInteger tokenTypeOf TkBytes{} = TypeBytes tokenTypeOf TkBytesBegin{} = TypeBytesIndef tokenTypeOf TkString{} = TypeString From 2e5e60fc788277f8ea4cdfa52b8e426ae754c0d4 Mon Sep 17 00:00:00 2001 From: Damian Nadales Date: Thu, 31 Aug 2023 18:44:27 +0200 Subject: [PATCH 2/3] Use an explicit literal --- cborg/src/Codec/CBOR/FlatTerm.hs | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/cborg/src/Codec/CBOR/FlatTerm.hs b/cborg/src/Codec/CBOR/FlatTerm.hs index 0036d5b..cea2ebf 100644 --- a/cborg/src/Codec/CBOR/FlatTerm.hs +++ b/cborg/src/Codec/CBOR/FlatTerm.hs @@ -562,28 +562,28 @@ fromFlatTerm decoder ft = -- | Map a 'TermToken' to the underlying CBOR 'TokenType' tokenTypeOf :: TermToken -> TokenType tokenTypeOf (TkInt n) - | n >= 0 = TypeUInt - | otherwise = TypeNInt + | n >= 0 = TypeUInt + | otherwise = TypeNInt tokenTypeOf (TkInteger n) -- See https://github.com/well-typed/cborg/issues/324 - | n >= 0 && n < 2^64 = TypeUInt64 - | n < 0 && n > -2^64 = TypeNInt64 - | otherwise = TypeInteger -tokenTypeOf TkBytes{} = TypeBytes -tokenTypeOf TkBytesBegin{} = TypeBytesIndef -tokenTypeOf TkString{} = TypeString -tokenTypeOf TkStringBegin{} = TypeStringIndef -tokenTypeOf TkListLen{} = TypeListLen -tokenTypeOf TkListBegin{} = TypeListLenIndef -tokenTypeOf TkMapLen{} = TypeMapLen -tokenTypeOf TkMapBegin{} = TypeMapLenIndef -tokenTypeOf TkTag{} = TypeTag -tokenTypeOf TkBool{} = TypeBool -tokenTypeOf TkNull = TypeNull -tokenTypeOf TkBreak = TypeBreak -tokenTypeOf TkSimple{} = TypeSimple -tokenTypeOf TkFloat16{} = TypeFloat16 -tokenTypeOf TkFloat32{} = TypeFloat32 -tokenTypeOf TkFloat64{} = TypeFloat64 + | 0 <= n && n <= 0xffffffffffffffff = TypeUInt64 + | -0xffffffffffffffff <= n && n < 0 = TypeNInt64 + | otherwise = TypeInteger +tokenTypeOf TkBytes{} = TypeBytes +tokenTypeOf TkBytesBegin{} = TypeBytesIndef +tokenTypeOf TkString{} = TypeString +tokenTypeOf TkStringBegin{} = TypeStringIndef +tokenTypeOf TkListLen{} = TypeListLen +tokenTypeOf TkListBegin{} = TypeListLenIndef +tokenTypeOf TkMapLen{} = TypeMapLen +tokenTypeOf TkMapBegin{} = TypeMapLenIndef +tokenTypeOf TkTag{} = TypeTag +tokenTypeOf TkBool{} = TypeBool +tokenTypeOf TkNull = TypeNull +tokenTypeOf TkBreak = TypeBreak +tokenTypeOf TkSimple{} = TypeSimple +tokenTypeOf TkFloat16{} = TypeFloat16 +tokenTypeOf TkFloat32{} = TypeFloat32 +tokenTypeOf TkFloat64{} = TypeFloat64 -------------------------------------------------------------------------------- From 8da252dfb5b0785580b1902161c8a9b56e65d3f3 Mon Sep 17 00:00:00 2001 From: Damian Nadales Date: Mon, 4 Sep 2023 19:10:03 +0200 Subject: [PATCH 3/3] fixup! Use an explicit literal --- cborg/src/Codec/CBOR/FlatTerm.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cborg/src/Codec/CBOR/FlatTerm.hs b/cborg/src/Codec/CBOR/FlatTerm.hs index cea2ebf..5d43e63 100644 --- a/cborg/src/Codec/CBOR/FlatTerm.hs +++ b/cborg/src/Codec/CBOR/FlatTerm.hs @@ -565,7 +565,7 @@ tokenTypeOf (TkInt n) | n >= 0 = TypeUInt | otherwise = TypeNInt tokenTypeOf (TkInteger n) -- See https://github.com/well-typed/cborg/issues/324 - | 0 <= n && n <= 0xffffffffffffffff = TypeUInt64 + | 0 <= n && n <= 0xffffffffffffffff = TypeUInt64 -- 0xffffffffffffffff == 2^64 - 1 | -0xffffffffffffffff <= n && n < 0 = TypeNInt64 | otherwise = TypeInteger tokenTypeOf TkBytes{} = TypeBytes