summaryrefslogtreecommitdiffstatshomepage
path: root/py/objnamedtuple.c
diff options
context:
space:
mode:
authorLars Haulin <lars.haulin@gmail.com>2022-07-08 17:48:40 +0200
committerDamien George <damien@micropython.org>2022-07-13 16:25:35 +1000
commit5bf3765631e645412dbd62a616cfdadeca5ea0c3 (patch)
treec8da4965e5391990e111162603dbfedc288c6a58 /py/objnamedtuple.c
parent2076f2efccfd86977f63de15958c142365c31f3b (diff)
downloadmicropython-5bf3765631e645412dbd62a616cfdadeca5ea0c3.tar.gz
micropython-5bf3765631e645412dbd62a616cfdadeca5ea0c3.zip
py/objnamedtuple: Fix segfault with empty namedtuple.
The empty tuple is usually a constant object, but named tuples must be allocated to allow modification. Added explicit allocation to fix this. Also added a regression test to verify creating an empty named tuple works. Fixes issue #7870. Signed-off-by: Lars Haulin <lars.haulin@gmail.com>
Diffstat (limited to 'py/objnamedtuple.c')
-rw-r--r--py/objnamedtuple.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c
index 214cad2572..b212993506 100644
--- a/py/objnamedtuple.c
+++ b/py/objnamedtuple.c
@@ -108,9 +108,10 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
#endif
}
- // Create a tuple and set the type to this namedtuple
- mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, NULL));
- tuple->base.type = type_in;
+ // Create a namedtuple with explicit malloc. Calling mp_obj_new_tuple
+ // with num_fields=0 returns a read-only object.
+ mp_obj_tuple_t *tuple = mp_obj_malloc_var(mp_obj_tuple_t, mp_obj_t, num_fields, type_in);
+ tuple->len = num_fields;
// Copy the positional args into the first slots of the namedtuple
memcpy(&tuple->items[0], args, sizeof(mp_obj_t) * n_args);