diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-09-18 17:39:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-18 17:39:27 (GMT) |
commit | 74a7f5d2dacd4c05aad0e64a275dae97d18f5355 (patch) | |
tree | d654c4b110caa5290f650689af46d6d6f8a8d59e /Include | |
parent | 0620bc7f78782cd2e400827aaf834f358250778f (diff) | |
download | cpython-74a7f5d2dacd4c05aad0e64a275dae97d18f5355.zip cpython-74a7f5d2dacd4c05aad0e64a275dae97d18f5355.tar.gz cpython-74a7f5d2dacd4c05aad0e64a275dae97d18f5355.tar.bz2 |
[3.12] gh-109496: Detect Py_DECREF() after dealloc in debug mode (GH-109539) (#109545)
gh-109496: Detect Py_DECREF() after dealloc in debug mode (GH-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.
(cherry picked from commit 0bb0d88e2d4e300946e399e088e2ff60de2ccf8c)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Include')
-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 77434e3..5c30c77 100644 --- a/Include/object.h +++ b/Include/object.h @@ -679,17 +679,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); } } |