diff options
author | Victor Stinner <vstinner@python.org> | 2023-09-18 14:59:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-18 14:59:09 (GMT) |
commit | 0bb0d88e2d4e300946e399e088e2ff60de2ccf8c (patch) | |
tree | e197a96bdf8c45937a2f839fd723a9bb58270c47 /Include/object.h | |
parent | ef659b96169888e52b6ff03ce28fffaaa8f76818 (diff) | |
download | cpython-0bb0d88e2d4e300946e399e088e2ff60de2ccf8c.zip cpython-0bb0d88e2d4e300946e399e088e2ff60de2ccf8c.tar.gz cpython-0bb0d88e2d4e300946e399e088e2ff60de2ccf8c.tar.bz2 |
gh-109496: Detect Py_DECREF() after dealloc in debug mode (#109539)
On a Python built in debug mode, Py_DECREF() now calls
_Py_NegativeRefcount() if the object is a dangling pointer to
deallocated memory: memory filled with 0xDD "dead byte" by the debug
hook on memory allocators. The fix is to check the reference count
*before* checking for _Py_IsImmortal().
Add test_decref_freed_object() to test_capi.test_misc.
Diffstat (limited to 'Include/object.h')
-rw-r--r-- | Include/object.h | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/Include/object.h b/Include/object.h index 5bcb393..9058558 100644 --- a/Include/object.h +++ b/Include/object.h @@ -660,17 +660,15 @@ static inline void Py_DECREF(PyObject *op) { #elif defined(Py_REF_DEBUG) static inline void Py_DECREF(const char *filename, int lineno, PyObject *op) { + if (op->ob_refcnt <= 0) { + _Py_NegativeRefcount(filename, lineno, op); + } if (_Py_IsImmortal(op)) { return; } _Py_DECREF_STAT_INC(); _Py_DECREF_DecRefTotal(); - if (--op->ob_refcnt != 0) { - if (op->ob_refcnt < 0) { - _Py_NegativeRefcount(filename, lineno, op); - } - } - else { + if (--op->ob_refcnt == 0) { _Py_Dealloc(op); } } |