summaryrefslogtreecommitdiff
path: root/py/objfun.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2023-11-03 14:19:55 +1100
committerDamien George <damien@micropython.org>2023-11-03 16:03:18 +1100
commitb6a977848407a4ced45d118cf926bd915cc89dfb (patch)
tree372814eb7f8234ecc5ffc3a7112ad830e69784d9 /py/objfun.c
parentc85db05244ef6185fbb3c218c508ddd179830942 (diff)
py/misc: Change sizeof to offsetof for variable-length alloc.
This fixes the case where e.g. struct foo_t { mp_obj_t x; uint16_t y; char buf[]; }; will have `sizeof(struct foo_t)==8`, but `offsetof(struct foo_t, buf)==6`. When computing the size to allocate for `m_new_obj_var` we need to use offsetof to avoid over-allocating. This is important especially when it might cause it to spill over into another GC block. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py/objfun.c')
-rw-r--r--py/objfun.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/py/objfun.c b/py/objfun.c
index 94b33cd5b..e2136968b 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -215,7 +215,7 @@ mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args
// RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
// return NULL, then vm.c takes the needed action (either raise
// RuntimeError or fallback to stack allocation).
- code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
+ code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
if (!code_state) {
return NULL;
}
@@ -247,10 +247,10 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
// allocate state for locals and stack
mp_code_state_t *code_state = NULL;
#if MICROPY_ENABLE_PYSTACK
- code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
+ code_state = mp_pystack_alloc(offsetof(mp_code_state_t, state) + state_size);
#else
if (state_size > VM_MAX_STATE_ON_STACK) {
- code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
+ code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
if (code_state != NULL) {
memset(code_state->state, 0, state_size);
@@ -258,7 +258,7 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
#endif
}
if (code_state == NULL) {
- code_state = alloca(sizeof(mp_code_state_t) + state_size);
+ code_state = alloca(offsetof(mp_code_state_t, state) + state_size);
#if MICROPY_DEBUG_VM_STACK_OVERFLOW
memset(code_state->state, 0, state_size);
#endif
@@ -320,7 +320,7 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
#else
// free the state if it was allocated on the heap
if (state_size != 0) {
- m_del_var(mp_code_state_t, byte, state_size, code_state);
+ m_del_var(mp_code_state_t, state, byte, state_size, code_state);
}
#endif