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

Serialization of Integer fails for -1 #54

Open
jprupp opened this issue Feb 20, 2021 · 2 comments
Open

Serialization of Integer fails for -1 #54

jprupp opened this issue Feb 20, 2021 · 2 comments

Comments

@jprupp
Copy link

jprupp commented Feb 20, 2021

There are issues with this library and the default encoding of negative Integer.

Encoding with the cereal library:

λ :m + Data.Serialize Data.ByteString.Base16
λ let bs = encode (-1 :: Integer)
λ encodeBase16 bs
"00ffffffff"
λ decode bs :: Either String Integer
Right (-1)

Encoding with the bytes library:

λ let bs = runPutS $ serialize (-1 :: Integer)
λ encodeBase16 bs
"ff"
λ runGetS deserialize bs :: Either String Integer
Left "too few bytes\nFrom:\tdemandInput\n\n"
λ let bl = runPutL $ serialize (-1 :: Integer)
λ Data.ByteString.Lazy.Base16.encodeBase16 bl
"ff"
λ runGetL deserialize bl :: Integer
*** Exception: Data.Binary.Get.runGet at position 1: not enough bytes
CallStack (from HasCallStack):
  error, called at libraries/binary/src/Data/Binary/Get.hs:351:5 in binary-0.8.8.0:Data.Binary.Get
@ekmett
Copy link
Owner

ekmett commented May 23, 2023

This seems like a pretty big bug, TBH. I think we should definitely do a patch, which will necessarily change the format, so should require a major version bump. The question is what the right patch looks like. e.g. Transmit sign then data? Only for the Integer case? In general?

@jprupp
Copy link
Author

jprupp commented May 23, 2023

Probably make it do the same that the Cereal library does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants