From 8f064e469d551ab51a960cebf011e34af5e7fa58 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 25 May 2017 20:42:30 +1000 Subject: [PATCH] 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. --- py/emitbc.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/py/emitbc.c b/py/emitbc.c index 6d8db81bc6..ec12a62c6c 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -448,7 +448,19 @@ bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) { } void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) { + if (emit->pass == MP_PASS_SCOPE) { + return; + } + assert((mp_int_t)emit->stack_size + delta >= 0); emit->stack_size += delta; + if (emit->stack_size > emit->scope->stack_size) { + emit->scope->stack_size = emit->stack_size; + } + emit->last_emit_was_return_value = false; +} + +static inline void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) { + mp_emit_bc_adjust_stack_size(emit, stack_size_delta); } 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) { #endif } -STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) { - if (emit->pass == MP_PASS_SCOPE) { - return; - } - assert((mp_int_t)emit->stack_size + stack_size_delta >= 0); - emit->stack_size += stack_size_delta; - if (emit->stack_size > emit->scope->stack_size) { - emit->scope->stack_size = emit->stack_size; - } - emit->last_emit_was_return_value = false; -} - void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) { emit_bc_pre(emit, 0); if (emit->pass == MP_PASS_SCOPE) {