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

Large unsigned 64-bit integer literals are not generated correctly in Lua #837

Closed
dgelessus opened this issue Nov 30, 2020 · 1 comment
Closed

Comments

@dgelessus
Copy link
Contributor

When targetting Lua, the compiler currently writes all integer literals as decimal (unsurprisingly). When the value of a decimal integer doesn't fit into a signed 64-bit integer, Lua silently converts it to a float, losing precision. This causes problems with the way Kaitai Struct represents unsigned 64-bit integers in Lua (see #836) - such unsigned integers are actually represented as negative signed 64-bit integers to maintain precision. As a result, equality checks between unsigned 64-bit integers read from a file and ones written literally in a KSY currently fail. This problem can be seen in the integers test (generated Lua code):

Message: spec/lua/test_integers.lua:15: expected: 1.844674407371e+19, actual: -1

The solution would be to write large unsigned 64-bit integers (that are outside of the signed 64-bit integer range) as hexadecimal literals instead. Unlike decimal literals, Lua doesn't automatically convert hexadecimal literals to float, and instead wraps/overflows them in the same way that Kaitai Struct does when reading. A similar solution was also implemented for Java, which also doesn't have unsigned 64-bit integers and uses a similar workaround (see #835): kaitai-io/kaitai_struct_compiler@dbda9b7

Originally reported in kaitai-io/kaitai_struct_lua_runtime#13.

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

No branches or pull requests

2 participants