Skip to content

Commit 9c3442c

Browse files
authored
gh-101408: PyObject_GC_Resize should calculate preheader size. (gh-101741)
1 parent 0056701 commit 9c3442c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:c:func:`PyObject_GC_Resize` should calculate preheader size if needed.
2+
Patch by Dong-hee Na.

Modules/gcmodule.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,16 +2361,17 @@ PyVarObject *
23612361
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
23622362
{
23632363
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
2364+
const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
23642365
_PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2365-
if (basicsize > (size_t)PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
2366+
if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
23662367
return (PyVarObject *)PyErr_NoMemory();
23672368
}
2368-
2369-
PyGC_Head *g = AS_GC(op);
2370-
g = (PyGC_Head *)PyObject_Realloc(g, sizeof(PyGC_Head) + basicsize);
2371-
if (g == NULL)
2369+
char *mem = (char *)op - presize;
2370+
mem = (char *)PyObject_Realloc(mem, presize + basicsize);
2371+
if (mem == NULL) {
23722372
return (PyVarObject *)PyErr_NoMemory();
2373-
op = (PyVarObject *) FROM_GC(g);
2373+
}
2374+
op = (PyVarObject *) (mem + presize);
23742375
Py_SET_SIZE(op, nitems);
23752376
return op;
23762377
}

0 commit comments

Comments
 (0)