Skip to content

Commit

Permalink
pythongh-101408: use _PyType_PreHeaderSize in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Feb 19, 2023
1 parent fdc1aa9 commit 0ef500e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
15 changes: 5 additions & 10 deletions Modules/_tracemalloc.c
Expand Up @@ -2,6 +2,7 @@
#include "pycore_fileutils.h" // _Py_write_noraise()
#include "pycore_gc.h" // PyGC_Head
#include "pycore_hashtable.h" // _Py_hashtable_t
#include "pycore_object.h" // _PyType_PreHeaderSize
#include "pycore_pymem.h" // _Py_tracemalloc_config
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_traceback.h"
Expand Down Expand Up @@ -1399,19 +1400,13 @@ static PyObject *
_tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
{
PyTypeObject *type;
void *ptr;
uintptr_t ptr;
traceback_t *traceback;

type = Py_TYPE(obj);
if (PyType_IS_GC(type)) {
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
}
else {
ptr = (void *)obj;
}
ptr = (uintptr_t)obj;
ptr -= _PyType_PreHeaderSize(Py_TYPE(obj));

traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (uintptr_t)ptr);
traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, ptr);
if (traceback == NULL)
Py_RETURN_NONE;

Expand Down
11 changes: 6 additions & 5 deletions Modules/gcmodule.c
Expand Up @@ -2348,17 +2348,18 @@ _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
PyVarObject *
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
{
size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
_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 = 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;
}
Expand Down
12 changes: 3 additions & 9 deletions Objects/object.c
Expand Up @@ -2342,15 +2342,9 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
/* Display the traceback where the object has been allocated.
Do it before dumping repr(obj), since repr() is more likely
to crash than dumping the traceback. */
void *ptr;
PyTypeObject *type = Py_TYPE(obj);
if (_PyType_IS_GC(type)) {
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
}
else {
ptr = (void *)obj;
}
_PyMem_DumpTraceback(fileno(stderr), ptr);
char *mem = (char *)obj;
mem -= _PyType_PreHeaderSize(Py_TYPE(obj));
_PyMem_DumpTraceback(fileno(stderr), mem);

/* This might succeed or fail, but we're about to abort, so at least
try to provide any extra info we can: */
Expand Down

0 comments on commit 0ef500e

Please sign in to comment.