sjmulder/nbt-js

64-bit integers overflow

sjmulder opened this issue · 6 comments

Values from TAG_Long tags may overflow if the value is negative or if it does not fit in 32 bits. This is a limitation of the library used for unpacking binary values.

Found this other NBT parsing library: https://github.com/maxogden/minecraft-nbt - it also has (about to fix) problems with TAG_Long, but the jdataview https://github.com/jdataview/jdataview library it uses supports 64-bit integers by splitting them into hi and lo fields (32-bit each, allows representing integers beyond 53-bits).

That looks like a good solution. You can submit a pull request if you feel like it, otherwise I’ll have a look soon.

Actually, maybe this should be configurable in some way. It really depends on the consumer what the desired output his. hi and lo fields are one good options, but other code may want to have a clamped value or even a string.

Hm, are there really situations where clamped values would be useful? Most 64-bit integers in NBT seem to be some kind of timestamp or identifier, where clamping could be problematic. Rounding would be problematic for identifiers, but possibly useful for timestamps.

Maybe valueOf could be implemented to return the approximate value (double, if coerced numerically), toString the precise decimal value as a string, and properties for the high and low halves.

@sjmulder Found this nifty module https://www.npmjs.org/package/node-int64 - looks like it solves all these problems, let me know what you think. Submitted PR to use this module in #9

That looks like a good solution, I’m surprised that the big values can be coerced to numbers. Thanks so much!