]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-101408: PyObject_GC_Resize should calculate preheader size. (gh-101741)
authorDong-hee Na <donghee.na@python.org>
Sun, 23 Apr 2023 17:18:49 +0000 (11:18 -0600)
committerGitHub <noreply@github.com>
Sun, 23 Apr 2023 17:18:49 +0000 (11:18 -0600)
Misc/NEWS.d/next/C API/2023-02-09-23-09-29.gh-issue-101408._paFIF.rst [new file with mode: 0644]
Modules/gcmodule.c

diff --git a/Misc/NEWS.d/next/C API/2023-02-09-23-09-29.gh-issue-101408._paFIF.rst b/Misc/NEWS.d/next/C API/2023-02-09-23-09-29.gh-issue-101408._paFIF.rst
new file mode 100644 (file)
index 0000000..172d661
--- /dev/null
@@ -0,0 +1,2 @@
+:c:func:`PyObject_GC_Resize` should calculate preheader size if needed.
+Patch by Dong-hee Na.
index 1d00fc3e717788bea684514c4ac28f179040d916..966c1e615502ef80a1b3ac940f72a1335fcce93f 100644 (file)
@@ -2361,16 +2361,17 @@ PyVarObject *
 _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
 {
     const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
+    const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
     _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
-    if (basicsize > (size_t)PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
+    if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
         return (PyVarObject *)PyErr_NoMemory();
     }
-
-    PyGC_Head *g = AS_GC(op);
-    g = (PyGC_Head *)PyObject_Realloc(g,  sizeof(PyGC_Head) + basicsize);
-    if (g == NULL)
+    char *mem = (char *)op - presize;
+    mem = (char *)PyObject_Realloc(mem,  presize + basicsize);
+    if (mem == NULL) {
         return (PyVarObject *)PyErr_NoMemory();
-    op = (PyVarObject *) FROM_GC(g);
+    }
+    op = (PyVarObject *) (mem + presize);
     Py_SET_SIZE(op, nitems);
     return op;
 }