There was an error while loading. Please reload this page.
CodeType
1 parent ac023ea commit 22dd5b5Copy full SHA for 22dd5b5
2 files changed
Misc/NEWS.d/next/Core_and_Builtins/2026-06-23-23-48-54.gh-issue-151763.Eu8pYQ.rst
@@ -0,0 +1 @@
1
+Fixes possible crash on :class:`types.CodeType` deallocation.
Objects/codeobject.c
@@ -743,6 +743,10 @@ _PyCode_New(struct _PyCodeConstructor *con)
743
return NULL;
744
}
745
746
+#ifdef Py_GIL_DISABLED
747
+ co->_co_unique_id = _Py_INVALID_UNIQUE_ID;
748
+#endif
749
+
750
if (init_code(co, con) < 0) {
751
Py_DECREF(co);
752
@@ -2449,15 +2453,17 @@ code_dealloc(PyObject *self)
2449
2453
FT_CLEAR_WEAKREFS(self, co->co_weakreflist);
2450
2454
free_monitoring_data(co->_co_monitoring);
2451
2455
#ifdef Py_GIL_DISABLED
2452
- // 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];
2456
- if (entry != NULL) {
2457
- PyMem_Free(entry);
+ if (co->co_tlbc != NULL) {
+ // 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
+ }
2464
2465
+ PyMem_Free(co->co_tlbc);
2466
- PyMem_Free(co->co_tlbc);
2467
#endif
2468
PyObject_Free(co);
2469
0 commit comments