Skip to content

fix(codegen): check narrowed int32 operand - #1195

Open
1sgtpepper wants to merge 1 commit into
0xMiden:nextfrom
1sgtpepper:fix-int32-uint-narrowing-1182
Open

fix(codegen): check narrowed int32 operand#1195
1sgtpepper wants to merge 1 commit into
0xMiden:nextfrom
1sgtpepper:fix-int32-uint-narrowing-1182

Conversation

@1sgtpepper

Copy link
Copy Markdown

Closes #1182

int32_to_uint and try_int32_to_uint were duplicating the word below the value being narrowed before applying the unsigned range mask. With another live value below the operand, checked narrowing could reject a valid value or accept an out-of-range value based on the wrong stack word.

This changes both helpers to duplicate the top stack value before masking. The full-width n == 32 case uses a zero mask so the mask construction does not shift by 32.

Regression coverage compiles HIR snippets and executes them through the package evaluator:

  • checked casts from u32 and i32 to i1, u8, and u16;
  • overflowing u8 addition, which covers the try_int32_to_uint path;
  • live guard values below the checked operand, so the old dup.1 behavior fails on both valid and invalid inputs.

@1sgtpepper
1sgtpepper marked this pull request as ready for review June 18, 2026 23:44
@1sgtpepper
1sgtpepper force-pushed the fix-int32-uint-narrowing-1182 branch from a2cf5b6 to c116211 Compare June 21, 2026 20:44
Comment thread codegen/masm/src/emit/mod.rs Outdated
Comment thread tests/integration/src/codegen/int32.rs Outdated
Comment thread codegen/masm/src/emit/int32.rs Outdated
@1sgtpepper
1sgtpepper force-pushed the fix-int32-uint-narrowing-1182 branch from c3d8385 to b9a0e40 Compare July 11, 2026 00:08
@1sgtpepper

Copy link
Copy Markdown
Author

@bitwalker Could you approve the CI run? Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

int32_to_uint checks dup.1 instead of the value being narrowed

2 participants