Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2990,7 +2990,7 @@ uc_compiler_compile_for_count(uc_compiler_t *compiler, bool local, uc_token_t *p
uc_compiler_parse_consume(compiler, TK_RPAREN);

/* if we have a condition, jump back to it, else continue to the loop body */
if (cond_off)
if (test_off)
uc_compiler_emit_jmp_dest(compiler, 0, cond_off);

/* back patch skip address */
Expand Down
38 changes: 38 additions & 0 deletions tests/custom/99_bugs/58_for_loop_condition_at_function_start
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
A counting for loop without an initializer that opened a function body
placed its condition at bytecode offset 0, which the compiler mistook
for an absent condition. The loop then never jumped back to the test
and kept running after the condition had turned false.

-- Testcase --
{%
function count_down(n) {
for (; n > 0; n--) {
if (n < -2)
return "runaway";

print(n, "\n");
}

return "done";
}

function walk(n) {
for (; n > 1; n = int(n / 2))
if (n == 0)
return "runaway";

return n;
}

print(count_down(3), "\n");
print(walk(8), "\n");
%}
-- End --

-- Expect stdout --
3
2
1
done
1
-- End --
Loading