Skip to content

Explicit bool cast of u8/i8/u16/i16 does not truncate high bits #3057

Open

Description

@trackoor

Bug description

Explicitly casting a small integer (i8, u8, i16, u16) to bool via <bool>x, x as bool, or bool(x) emits i32.ne 0 without masking or sign-extending the operand first, so an overflowed value that wraps to 0 evaluates to true.

For a = 255 in the example below, c wraps to 0 in u8. Calling g(255) returns 0 (false), whereas f(255) returns 1 (true) because f compiles to i32.add followed directly by i32.ne 0 without i32.and 255. Both functions should return 0 (false).

This happens because convertExpression only wraps short integers when fromType.size < toType.size, skipping bool where toType.size == 1.

Steps to reproduce

export function f(a: u8): bool {
  let c: u8 = a + 1;
  return <bool>c;
}
export function g(a: u8): bool {
  let c: u8 = a + 1;
  return c ? true : false;
}

Credit: https://ucsbplse.github.io/

AssemblyScript version

v0.28.20

Activity

  1. changed the title [-]Explicit bool cast of u8/i8/u16/i16 does not truncate high bits[/-] [+]Small integer types (u8/i8/u16/i16) are not wrapped or truncated in bool casts and bitwise AND[/+] on Nov 8, 2026
  2. changed the title [-]Small integer types (u8/i8/u16/i16) are not wrapped or truncated in bool casts and bitwise AND[/-] [+]Explicit bool cast of u8/i8/u16/i16 does not truncate high bits[/+] on Nov 9, 2026
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