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 /extmod/modre.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 'extmod/modre.c')
-rw-r--r-- | extmod/modre.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/extmod/modre.c b/extmod/modre.c index 7f00b1c23..c24c07d09 100644 --- a/extmod/modre.c +++ b/extmod/modre.c @@ -207,12 +207,12 @@ STATIC mp_obj_t re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { subj.begin_line = subj.begin = mp_obj_str_get_data(args[1], &len); subj.end = subj.begin + len; int caps_num = (self->re.sub + 1) * 2; - mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, char *, caps_num); + mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, caps, char *, caps_num); // cast is a workaround for a bug in msvc: it treats const char** as a const pointer instead of a pointer to pointer to const char memset((char *)match->caps, 0, caps_num * sizeof(char *)); int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, is_anchored); if (res == 0) { - m_del_var(mp_obj_match_t, char *, caps_num, match); + m_del_var(mp_obj_match_t, caps, char *, caps_num, match); return mp_const_none; } |