[Python-checkins] bpo-45061: Detect Py_DECREF(Py_True) bug (GH-28089)

vstinner webhook-mailer at python.org
Tue Aug 31 12:05:23 EDT 2021


https://github.com/python/cpython/commit/4300352000beed22fb525ec45fd331918d206528
commit: 4300352000beed22fb525ec45fd331918d206528
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-08-31T18:05:15+02:00
summary:

bpo-45061: Detect Py_DECREF(Py_True) bug (GH-28089)

Add a deallocator to the bool type to detect refcount bugs in C
extensions which call Py_DECREF(Py_True) or Py_DECREF(Py_False) by
mistake.

files:
A Misc/NEWS.d/next/C API/2021-08-31-15-21-36.bpo-45061.ZH0HVe.rst
M Objects/boolobject.c
M Objects/object.c

diff --git a/Misc/NEWS.d/next/C API/2021-08-31-15-21-36.bpo-45061.ZH0HVe.rst b/Misc/NEWS.d/next/C API/2021-08-31-15-21-36.bpo-45061.ZH0HVe.rst
new file mode 100644
index 00000000000000..58bd534601fb9d
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2021-08-31-15-21-36.bpo-45061.ZH0HVe.rst	
@@ -0,0 +1,3 @@
+Add a deallocator to the :class:`bool` type to detect refcount bugs in C
+extensions which call ``Py_DECREF(Py_True);`` or ``Py_DECREF(Py_False);`` by
+mistake. Patch by Victor Stinner.
diff --git a/Objects/boolobject.c b/Objects/boolobject.c
index b786966533e1d6..bc1666f55717c1 100644
--- a/Objects/boolobject.c
+++ b/Objects/boolobject.c
@@ -153,6 +153,13 @@ static PyNumberMethods bool_as_number = {
     0,                          /* nb_index */
 };
 
+static void _Py_NO_RETURN
+bool_dealloc(PyObject* Py_UNUSED(ignore))
+{
+    Py_FatalError("deallocating True or False likely caused by "
+                  "a refcount bug in a C extension");
+}
+
 /* The type object for bool.  Note that this cannot be subclassed! */
 
 PyTypeObject PyBool_Type = {
@@ -160,7 +167,7 @@ PyTypeObject PyBool_Type = {
     "bool",
     sizeof(struct _longobject),
     0,
-    0,                                          /* tp_dealloc */
+    bool_dealloc,                               /* tp_dealloc */
     0,                                          /* tp_vectorcall_offset */
     0,                                          /* tp_getattr */
     0,                                          /* tp_setattr */
diff --git a/Objects/object.c b/Objects/object.c
index 446c974f8e614b..026262b5448495 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1560,14 +1560,11 @@ none_repr(PyObject *op)
     return PyUnicode_FromString("None");
 }
 
-/* ARGUSED */
 static void _Py_NO_RETURN
-none_dealloc(PyObject* ignore)
+none_dealloc(PyObject* Py_UNUSED(ignore))
 {
-    /* This should never get called, but we also don't want to SEGV if
-     * we accidentally decref None out of existence.
-     */
-    Py_FatalError("deallocating None");
+    Py_FatalError("deallocating None likely caused by a refcount bug "
+                  "in a C extension");
 }
 
 static PyObject *



More information about the Python-checkins mailing list