Skip to content

Fix byte comparison overflow in Ble/Bgt when value is 255#293

Merged
jonathanpeppers merged 2 commits intomainfrom
crypto-sample-fixes
Mar 20, 2026
Merged

Fix byte comparison overflow in Ble/Bgt when value is 255#293
jonathanpeppers merged 2 commits intomainfrom
crypto-sample-fixes

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Owner

@jonathanpeppers jonathanpeppers commented Mar 20, 2026

When Ble/Bgt compares a byte against 255, the adjustValue: 1 produces 256 which overflows the checked((byte)(...)) cast, crashing the transpiler with OverflowException.

Fix

EmitBranchCompare now returns bool -- false when the compare value exceeds byte range. Callers handle the trivially-true/false cases:

  • Ble (x <= 255): always true for bytes, emit unconditional JMP
  • Bgt (x > 255): always false for bytes, skip branch entirely

Context

Discovered while working on the crypto sample (PR #211), which has if (x <= 0xFF) patterns.

All 551 existing tests pass.

When Ble/Bgt compares a byte against 255, the adjust+1 produces 256 which
overflows the checked byte cast. Fix by returning false from EmitBranchCompare
when the compare value exceeds byte range, letting callers emit unconditional
JMP (Ble: x<=255 always true) or skip the branch (Bgt: x>255 always false).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a transpiler crash in EmitBranchCompare when Ble/Bgt comparisons against 255 overflow the byte range after applying adjustValue: 1, by allowing callers to handle the “always true/false for byte” edge case instead of throwing OverflowException.

Changes:

  • Changed EmitBranchCompare to return bool and detect out-of-byte-range compare values.
  • Updated Ble to emit an unconditional JMP when the comparison is trivially true (x <= 255).
  • Updated Bgt to skip emitting the branch when the comparison is trivially false (x > 255).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/dotnes.tasks/Utilities/IL2NESWriter.ILDispatch.cs Handles the Ble/Bgt overflow case by emitting unconditional jump / no-op instead of crashing.
src/dotnes.tasks/Utilities/IL2NESWriter.Arithmetic.cs Changes EmitBranchCompare to return bool and avoid checked(byte) overflow by range-checking.

Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.Arithmetic.cs
Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.ILDispatch.cs
Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.Arithmetic.cs Outdated
@jonathanpeppers jonathanpeppers changed the title Fix EmitBranchCompare overflow when comparing against 255 Fix byte comparison overflow in Ble/Bgt when value is 255 Mar 20, 2026
…, typo fix

- Bne/Beq/Blt/Bge callers now throw TranspileException if EmitBranchCompare
  returns false (comparison value exceeds byte range)
- Added ByteComparison_LessThanOrEqual255 regression test
- Fixed comment typo in Arithmetic.cs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Owner Author

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 3 review comments addressed in e9d68cb. All 552 tests pass including the new ByteComparison_LessThanOrEqual255 regression test.

@jonathanpeppers jonathanpeppers merged commit b514ef9 into main Mar 20, 2026
1 check passed
@jonathanpeppers jonathanpeppers deleted the crypto-sample-fixes branch March 20, 2026 03:30
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.

2 participants