Skip to content

Commit 22dd5b5

Browse files
authored
gh-151763: Fix possible crash on CodeType deallocation (#152034)
1 parent ac023ea commit 22dd5b5

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes possible crash on :class:`types.CodeType` deallocation.

Objects/codeobject.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ _PyCode_New(struct _PyCodeConstructor *con)
743743
return NULL;
744744
}
745745

746+
#ifdef Py_GIL_DISABLED
747+
co->_co_unique_id = _Py_INVALID_UNIQUE_ID;
748+
#endif
749+
746750
if (init_code(co, con) < 0) {
747751
Py_DECREF(co);
748752
return NULL;
@@ -2449,15 +2453,17 @@ code_dealloc(PyObject *self)
24492453
FT_CLEAR_WEAKREFS(self, co->co_weakreflist);
24502454
free_monitoring_data(co->_co_monitoring);
24512455
#ifdef Py_GIL_DISABLED
2452-
// The first element always points to the mutable bytecode at the end of
2453-
// the code object, which will be freed when the code object is freed.
2454-
for (Py_ssize_t i = 1; i < co->co_tlbc->size; i++) {
2455-
char *entry = co->co_tlbc->entries[i];
2456-
if (entry != NULL) {
2457-
PyMem_Free(entry);
2456+
if (co->co_tlbc != NULL) {
2457+
// The first element always points to the mutable bytecode at the end of
2458+
// the code object, which will be freed when the code object is freed.
2459+
for (Py_ssize_t i = 1; i < co->co_tlbc->size; i++) {
2460+
char *entry = co->co_tlbc->entries[i];
2461+
if (entry != NULL) {
2462+
PyMem_Free(entry);
2463+
}
24582464
}
2465+
PyMem_Free(co->co_tlbc);
24592466
}
2460-
PyMem_Free(co->co_tlbc);
24612467
#endif
24622468
PyObject_Free(co);
24632469
}

0 commit comments

Comments
 (0)