compiler: do not drop the loop condition at chunk offset 0 - #469
Merged
Merged
Conversation
uc_compiler_compile_for_count() records where the condition starts in
cond_off and treats 0 as "no condition". A counting for loop without an
initializer that opens a function body puts its condition at offset 0,
so the jump from the incrementer back to the condition is never emitted
and the loop keeps running after the condition turned false:
function f(n) { for (; n > 0; n--) print(n); }
never returns. Test for the conditional jump instead, which is always
emitted after at least one instruction of the condition.
Fixes: 37568066a7d9 ("treewide: rewrite ucode interpreter")
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
dangowrt
marked this pull request as ready for review
October 5, 2026 23:49
Collaborator
|
Merged, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A counting
forloop without an initializer that opens a function body never re-evaluates its condition. uc_compiler_compile_for_count() records where the condition starts incond_offand treats 0 as "no condition", but when the loop is the first code in a function its condition does start at chunk offset 0, so the jump from the incrementer back to the condition is never emitted and the loop runs on after the condition has turned false:prints 3, 2, 1, 0, -1, ... and never returns. Any statement before the loop, even
let x;, moves the condition off offset 0 and hides the bug, which is why the existing for-loop test does not catch it.The fix tests for the conditional jump instead, which is always emitted after at least one instruction of the condition and so is never at offset 0. The new regression test in 99_bugs fails with "runaway" on the old compiler instead of hanging, and the whole custom test suite passes.