diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2023-11-03 14:19:55 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-11-03 16:03:18 +1100 |
commit | b6a977848407a4ced45d118cf926bd915cc89dfb (patch) | |
tree | 372814eb7f8234ecc5ffc3a7112ad830e69784d9 /py/modthread.c | |
parent | c85db05244ef6185fbb3c218c508ddd179830942 (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/modthread.c')
-rw-r--r-- | py/modthread.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/py/modthread.c b/py/modthread.c index e4dcccd25..3a8a1e03c 100644 --- a/py/modthread.c +++ b/py/modthread.c @@ -235,7 +235,7 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args) // check for keyword arguments if (n_args == 2) { // just position arguments - th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len); + th_args = m_new_obj_var(thread_entry_args_t, args, mp_obj_t, pos_args_len); th_args->n_kw = 0; } else { // positional and keyword arguments @@ -243,7 +243,7 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args) mp_raise_TypeError(MP_ERROR_TEXT("expecting a dict for keyword args")); } mp_map_t *map = &((mp_obj_dict_t *)MP_OBJ_TO_PTR(args[2]))->map; - th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len + 2 * map->used); + th_args = m_new_obj_var(thread_entry_args_t, args, mp_obj_t, pos_args_len + 2 * map->used); th_args->n_kw = map->used; // copy across the keyword arguments for (size_t i = 0, n = pos_args_len; i < map->alloc; ++i) { |