Skip to content

Copy/paste bug in ReadU64LEB128 #2192

Closed

Description

@kssreeram

In file wabt/src/leb128.cc, function ReadU64Leb128:

This snippet is incorrect. It appears to have been copied from ReadU32Leb128:

else if (p + 9 < end && (p[9] & 0x80) == 0) {
  // The top bits set represent values > 32 bits.
  if (p[9] & 0xf0) {
    return 0;
  }
  *out_value = LEB128_10(uint64_t);
  return 10;
}

The right code is:

else if (p + 9 < end && (p[9] & 0x80) == 0) {
  // The top bits set represent values > 64 bits.
  if (p[9] & 0xfe) {
    return 0;
  }
  *out_value = LEB128_10(uint64_t);
  return 10;
}

The number of used bits in byte 10 equals 64 - 9*7 = 1 bit. Only one bit in the final byte is a part of the 64-bit value. Therefore, all the remaining 7 bits of the last byte must be zero. The check for that is if (p[9] & 0xfe) {.

Activity

  1. keithw commented on Apr 12, 2023

    @keithw
    Member

    Thank you! This sounds reasonable but I haven't double-checked this myself -- are you up for submitting a PR with a regression test (please see the test/regress directory for some examples)?

  2. kssreeram commented on Apr 21, 2023

    @kssreeram
    Author

    I'll give it a shot this weekend.

  3. added a commit that references this issue on Jun 9, 2023
    8ffd61f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions