gh-101408: PyObject_GC_Resize should calculate preheader size. (gh-101741)

This commit is contained in:
Dong-hee Na 2023-04-23 11:18:49 -06:00 committed by GitHub
parent 0056701aa3
commit 9c3442c093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -0,0 +1,2 @@
:c:func:`PyObject_GC_Resize` should calculate preheader size if needed.
Patch by Dong-hee Na.

View File

@ -2361,16 +2361,17 @@ PyVarObject *
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
{ {
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), 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)); _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(); return (PyVarObject *)PyErr_NoMemory();
} }
char *mem = (char *)op - presize;
PyGC_Head *g = AS_GC(op); mem = (char *)PyObject_Realloc(mem, presize + basicsize);
g = (PyGC_Head *)PyObject_Realloc(g, sizeof(PyGC_Head) + basicsize); if (mem == NULL) {
if (g == NULL)
return (PyVarObject *)PyErr_NoMemory(); return (PyVarObject *)PyErr_NoMemory();
op = (PyVarObject *) FROM_GC(g); }
op = (PyVarObject *) (mem + presize);
Py_SET_SIZE(op, nitems); Py_SET_SIZE(op, nitems);
return op; return op;
} }