natalie
natalie copied to clipboard
Bug in Marshal.dump/Marshal.load with integers
$ 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.
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.
https://rubyreferences.github.io/rubyref/builtin/marshal.html#bignum