Skip to content

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

Closed
@dgelessus

Description

@dgelessus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions