natalie icon indicating copy to clipboard operation
natalie copied to clipboard

Bug in Marshal.dump/Marshal.load with integers

Open herwinw opened this issue 11 months ago • 2 comments

$ ruby -e 'puts Marshal.load(Marshal.dump((1 << 32) - 1))'; bin/natalie -e 'puts Marshal.load(Marshal.dump((1 << 32) - 1))'
4294967295
4294967295

$ ruby -e 'puts Marshal.load(Marshal.dump(1 << 32))'; bin/natalie -e 'puts Marshal.load(Marshal.dump(1 << 32))'
4294967296
0

Our code works fine with 32 bit signed ints, but it should switch to a different data type as soon as we need a 5th byte. At this point MRI switches over to the l identifier instead of i (long instead of int?). We keep using i, which causes the parser to err.

herwinw avatar Mar 17 '24 08:03 herwinw

https://iliabylich.github.io/2016/01/25/ruby-marshalling-from-a-to-z.html#integer Some information on the Marshal format. Too bad none of the write ups I could find mentioned larger integers.

herwinw avatar Mar 17 '24 08:03 herwinw

https://rubyreferences.github.io/rubyref/builtin/marshal.html#bignum

herwinw avatar Mar 19 '24 17:03 herwinw