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

vlq_base128_le incorrectrly compiled in C# #559

Closed
mikhail-khalizev opened this issue Nov 10, 2021 · 2 comments
Closed

vlq_base128_le incorrectrly compiled in C# #559

mikhail-khalizev opened this issue Nov 10, 2021 · 2 comments
Labels

Comments

@mikhail-khalizev
Copy link

mikhail-khalizev commented Nov 10, 2021

Hi.

Source file: common/vlq_base128_le.ksy

Look in screenshot below, it is source file compiled for C#.
Since by default csharp uses Int32 type, shifts by '35', '42', '49' computes incorrectly.

image

Possible workaround to cast values to u8 in vlq_base128_le.ksy:

  value:
    value: >-
      (groups[0].value.as<u8>
      + (len >= 2 ? (groups[1].value.as<u8> << 7) : 0)
      + (len >= 3 ? (groups[2].value.as<u8> << 14) : 0)
      + (len >= 4 ? (groups[3].value.as<u8> << 21) : 0)
      + (len >= 5 ? (groups[4].value.as<u8> << 28) : 0)
      + (len >= 6 ? (groups[5].value.as<u8> << 35) : 0)
      + (len >= 7 ? (groups[6].value.as<u8> << 42) : 0)
      + (len >= 8 ? (groups[7].value.as<u8> << 49) : 0)).as<u8>
@yav
Copy link

yav commented Oct 5, 2023

I just run into this issue as well, but when experimenting with the C++ backend. I can't seem to find any information on the type inference algorithm for Kaitai structs, so I am not sure if this is a bug in the compiler or the spec, but fixing it as suggested above seems reasonable to me.

The current status, where the spec looks simple, but when you try to parse some large numbers you get the wrong result is pretty unfortunate.

@generalmimon
Copy link
Member

generalmimon commented Oct 5, 2023

@yav:

I can't seem to find any information on the type inference algorithm for Kaitai structs

See kaitai-io/kaitai_struct#257 (comment).

but fixing it as suggested above seems reasonable to me.

The current status, where the spec looks simple, but when you try to parse some large numbers you get the wrong result is pretty unfortunate.

I agree. The workaround by @mikhail-khalizev looks solid to me; let me apply it...

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

No branches or pull requests

3 participants