]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-9263: Fix _PyObject_Dump() for freed object (#10661)
authorVictor Stinner <vstinner@redhat.com>
Thu, 22 Nov 2018 15:32:57 +0000 (16:32 +0100)
committerGitHub <noreply@github.com>
Thu, 22 Nov 2018 15:32:57 +0000 (16:32 +0100)
If _PyObject_Dump() detects that the object is freed, don't try to
dump it (exit immediately).

Enhance also _PyObject_IsFreed(): it now detects if the pointer
itself looks like freed memory.

Objects/object.c

index 9d2614bb6d110ac450193bbb2c45ca2df729f3d3..c2d78aa47e65c562785f1571c1ea402361e42446 100644 (file)
@@ -423,6 +423,10 @@ _Py_BreakPoint(void)
 int
 _PyObject_IsFreed(PyObject *op)
 {
+    uintptr_t ptr = (uintptr_t)op;
+    if (_PyMem_IsFreed(&ptr, sizeof(ptr))) {
+        return 1;
+    }
     int freed = _PyMem_IsFreed(&op->ob_type, sizeof(op->ob_type));
     /* ignore op->ob_ref: the value can have be modified
        by Py_INCREF() and Py_DECREF(). */
@@ -448,6 +452,7 @@ _PyObject_Dump(PyObject* op)
         /* It seems like the object memory has been freed:
            don't access it to prevent a segmentation fault. */
         fprintf(stderr, "<freed object>\n");
+        return;
     }
 
     PyGILState_STATE gil;