diff options
author | Dong-hee Na <donghee.na@python.org> | 2023-04-23 17:18:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-23 17:18:49 (GMT) |
commit | 9c3442c0938127788fa59e4ceb80ae78b86fad1d (patch) | |
tree | 2feaf5b0c26549a1cdf360d2931274fc88dea5fa /Modules | |
parent | 0056701aa370553636b676cc99e327137d1688c6 (diff) | |
download | cpython-9c3442c0938127788fa59e4ceb80ae78b86fad1d.zip cpython-9c3442c0938127788fa59e4ceb80ae78b86fad1d.tar.gz cpython-9c3442c0938127788fa59e4ceb80ae78b86fad1d.tar.bz2 |
gh-101408: PyObject_GC_Resize should calculate preheader size. (gh-101741)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/gcmodule.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index 1d00fc3..966c1e6 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -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; } |