Skip to content

Commit 71044a4

Browse files
projectgusdpgeorge
authored andcommitted
py/parse: Zero out dangling parse tree pointer to fix potential GC leak.
This fixes a bug where a random Python object may become un-garbage-collectable until an enclosing Python file (compiled on device) finishes executing. Details: The mp_parse_tree_t structure is stored on the stack in top-level functions such as parse_compile_execute() in pyexec.c (and others). Although it quickly falls out of scope in these functions, it is usually still in the current stack frame when the compiled code executes. (Compiler dependent, but usually it's one stack push per function.) This means if any Python object happens to allocate at the same address as the (freed) root parse tree chunk, it's un-garbage-collectable as there's a (dangling) pointer up the stack referencing this same address. As reported by @GitHubsSilverBullet here: https://github.com/orgs/micropython/discussions/14116#discussioncomment-8837214 This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 9d27183 commit 71044a4

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

py/parse.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@ void mp_parse_tree_clear(mp_parse_tree_t *tree) {
13861386
m_del(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc);
13871387
chunk = next;
13881388
}
1389+
tree->chunk = NULL; // Avoid dangling pointer that may live on stack
13891390
}
13901391

13911392
#endif // MICROPY_ENABLE_COMPILER

0 commit comments

Comments
 (0)