Skip to content

Commit 58b56b9

Browse files
EmilieNumworksdpgeorge
authored andcommitted
py/qstr: Reset mpstate.qstr_last_chunk before raising an error.
The qstr_last_chunk is not collected by the garbage collector. This relies on the assertion that qstr_pool_t also references the qstr_last_chunk. If an exception is raised while allocating the qstr_pool_t, qstr_last_chunk has to be invalidated not to become a dangling reference at the next garbage collection. Signed-off-by: Emilie Feral <emilie.feral@numworks.com>
1 parent 01953f2 commit 58b56b9

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

py/qstr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ STATIC qstr qstr_add(const byte *q_ptr) {
153153
#endif
154154
qstr_pool_t *pool = m_new_obj_var_maybe(qstr_pool_t, const char *, new_alloc);
155155
if (pool == NULL) {
156+
// Keep qstr_last_chunk consistent with qstr_pool_t: qstr_last_chunk is not scanned
157+
// at garbage collection since it's reachable from a qstr_pool_t. And the caller of
158+
// this function expects q_ptr to be stored in a qstr_pool_t so it can be reached
159+
// by the collector. If qstr_pool_t allocation failed, qstr_last_chunk needs to be
160+
// NULL'd. Otherwise it may become a dangling pointer at the next garbage collection.
161+
MP_STATE_VM(qstr_last_chunk) = NULL;
156162
QSTR_EXIT();
157163
m_malloc_fail(new_alloc);
158164
}

0 commit comments

Comments
 (0)