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>
This commit is contained in:
Jim Mussared
2023-11-03 14:19:55 +11:00
committed by Damien George
parent c85db05244
commit b6a9778484
11 changed files with 24 additions and 24 deletions

View File

@@ -264,7 +264,7 @@ void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items) {
void mp_obj_tuple_del(mp_obj_t self_in) {
assert(mp_obj_is_type(self_in, &mp_type_tuple));
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
m_del_var(mp_obj_tuple_t, items, mp_obj_t, self->len, self);
}
/******************************************************************************/