[3.15] gh-151763: Fix possible crash on CodeType deallocation (GH-152034) (#152069)

gh-151763: Fix possible crash on `CodeType` deallocation (GH-152034)
(cherry picked from commit 22dd5b5b37)

Co-authored-by: sobolevn <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2026-06-24 14:17:41 +02:00 committed by GitHub
parent 6bad84d64c
commit e8b34391e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 7 deletions

View file

@ -743,6 +743,10 @@ _PyCode_New(struct _PyCodeConstructor *con)
return NULL;
}
#ifdef Py_GIL_DISABLED
co->_co_unique_id = _Py_INVALID_UNIQUE_ID;
#endif
if (init_code(co, con) < 0) {
Py_DECREF(co);
return NULL;
@ -2449,15 +2453,17 @@ code_dealloc(PyObject *self)
FT_CLEAR_WEAKREFS(self, co->co_weakreflist);
free_monitoring_data(co->_co_monitoring);
#ifdef Py_GIL_DISABLED
// The first element always points to the mutable bytecode at the end of
// the code object, which will be freed when the code object is freed.
for (Py_ssize_t i = 1; i < co->co_tlbc->size; i++) {
char *entry = co->co_tlbc->entries[i];
if (entry != NULL) {
PyMem_Free(entry);
if (co->co_tlbc != NULL) {
// The first element always points to the mutable bytecode at the end of
// the code object, which will be freed when the code object is freed.
for (Py_ssize_t i = 1; i < co->co_tlbc->size; i++) {
char *entry = co->co_tlbc->entries[i];
if (entry != NULL) {
PyMem_Free(entry);
}
}
PyMem_Free(co->co_tlbc);
}
PyMem_Free(co->co_tlbc);
#endif
PyObject_Free(co);
}