Skip to content

Commit 5b5562c

Browse files
committed
py: Fix stack underflow with optimised for loop.
1 parent 049a01d commit 5b5562c

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

py/compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
17451745
// And, if the loop never runs, the loop variable should never be assigned
17461746
void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
17471747
START_BREAK_CONTINUE_BLOCK
1748-
comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1748+
// note that we don't need to pop anything when breaking from an optimise for loop
17491749

17501750
uint top_label = comp_next_label(comp);
17511751
uint entry_label = comp_next_label(comp);

py/vm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args,
159159

160160
#if DETECT_VM_STACK_OVERFLOW
161161
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
162-
if (sp != state) {
163-
printf("Stack misalign: %d\n", sp - state);
162+
if (sp < state) {
163+
printf("VM stack underflow: " INT_FMT "\n", sp - state);
164164
assert(0);
165165
}
166166
}
@@ -178,7 +178,7 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args,
178178
}
179179
}
180180
if (overflow) {
181-
printf("VM stack overflow state=%p n_state+1=%u\n", state, n_state);
181+
printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", state, n_state);
182182
assert(0);
183183
}
184184
}

tests/basics/for_return.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# test returning from within a for loop
2+
3+
def f():
4+
for i in [1, 2, 3]:
5+
return i
6+
7+
print(f())

0 commit comments

Comments
 (0)