Skip to content

Commit 8f064e4

Browse files
committed
py/emitbc: Fix bug with BC emitter computing Python stack size.
Previous to this patch the mp_emit_bc_adjust_stack_size function would adjust the current stack size but would not increase the maximum stack size if the current size went above it. This meant that certain Python code (eg a try-finally block with no statements inside it) would not have enough Python stack allocated to it. This patch fixes the problem by always checking if the current stack size goes above the maximum, and adjusting the latter if it does.
1 parent 04d05db commit 8f064e4

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

py/emitbc.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,19 @@ bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
448448
}
449449

450450
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
451+
if (emit->pass == MP_PASS_SCOPE) {
452+
return;
453+
}
454+
assert((mp_int_t)emit->stack_size + delta >= 0);
451455
emit->stack_size += delta;
456+
if (emit->stack_size > emit->scope->stack_size) {
457+
emit->scope->stack_size = emit->stack_size;
458+
}
459+
emit->last_emit_was_return_value = false;
460+
}
461+
462+
static inline void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
463+
mp_emit_bc_adjust_stack_size(emit, stack_size_delta);
452464
}
453465

454466
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
@@ -471,18 +483,6 @@ void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
471483
#endif
472484
}
473485

474-
STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
475-
if (emit->pass == MP_PASS_SCOPE) {
476-
return;
477-
}
478-
assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
479-
emit->stack_size += stack_size_delta;
480-
if (emit->stack_size > emit->scope->stack_size) {
481-
emit->scope->stack_size = emit->stack_size;
482-
}
483-
emit->last_emit_was_return_value = false;
484-
}
485-
486486
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
487487
emit_bc_pre(emit, 0);
488488
if (emit->pass == MP_PASS_SCOPE) {

0 commit comments

Comments
 (0)