Skip to content

Commit 74d5f61

Browse files
authored
gh-99845: Clean up _PyObject_VAR_SIZE() usage (#99847)
* code_sizeof() now uses an unsigned type (size_t) to compute the result. * Fix _PyObject_ComputedDictPointer(): cast _PyObject_VAR_SIZE() to Py_ssize_t, rather than long: it's a different type on 64-bit Windows. * Clarify that _PyObject_VAR_SIZE() uses an unsigned type (size_t).
1 parent 4246fe9 commit 74d5f61

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

Modules/gcmodule.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -2329,15 +2329,14 @@ _PyObject_GC_New(PyTypeObject *tp)
23292329
PyVarObject *
23302330
_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
23312331
{
2332-
size_t size;
23332332
PyVarObject *op;
23342333

23352334
if (nitems < 0) {
23362335
PyErr_BadInternalCall();
23372336
return NULL;
23382337
}
23392338
size_t presize = _PyType_PreHeaderSize(tp);
2340-
size = _PyObject_VAR_SIZE(tp, nitems);
2339+
size_t size = _PyObject_VAR_SIZE(tp, nitems);
23412340
op = (PyVarObject *)gc_alloc(size, presize);
23422341
if (op == NULL) {
23432342
return NULL;
@@ -2351,7 +2350,7 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
23512350
{
23522351
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
23532352
_PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2354-
if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
2353+
if (basicsize > (size_t)PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
23552354
return (PyVarObject *)PyErr_NoMemory();
23562355
}
23572356

Objects/codeobject.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1867,15 +1867,13 @@ static PyGetSetDef code_getsetlist[] = {
18671867
static PyObject *
18681868
code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
18691869
{
1870-
Py_ssize_t res = _PyObject_VAR_SIZE(Py_TYPE(co), Py_SIZE(co));
1871-
1870+
size_t res = _PyObject_VAR_SIZE(Py_TYPE(co), Py_SIZE(co));
18721871
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
18731872
if (co_extra != NULL) {
1874-
res += sizeof(_PyCodeObjectExtra) +
1875-
(co_extra->ce_size-1) * sizeof(co_extra->ce_extras[0]);
1873+
res += sizeof(_PyCodeObjectExtra);
1874+
res += ((size_t)co_extra->ce_size - 1) * sizeof(co_extra->ce_extras[0]);
18761875
}
1877-
1878-
return PyLong_FromSsize_t(res);
1876+
return PyLong_FromSize_t(res);
18791877
}
18801878

18811879
static PyObject *

Objects/object.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -1043,22 +1043,25 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
10431043
PyObject **
10441044
_PyObject_ComputedDictPointer(PyObject *obj)
10451045
{
1046-
Py_ssize_t dictoffset;
10471046
PyTypeObject *tp = Py_TYPE(obj);
1048-
10491047
assert((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
1050-
dictoffset = tp->tp_dictoffset;
1051-
if (dictoffset == 0)
1048+
1049+
Py_ssize_t dictoffset = tp->tp_dictoffset;
1050+
if (dictoffset == 0) {
10521051
return NULL;
1052+
}
1053+
10531054
if (dictoffset < 0) {
10541055
assert(dictoffset != -1);
1056+
10551057
Py_ssize_t tsize = Py_SIZE(obj);
10561058
if (tsize < 0) {
10571059
tsize = -tsize;
10581060
}
10591061
size_t size = _PyObject_VAR_SIZE(tp, tsize);
1062+
assert(size <= (size_t)PY_SSIZE_T_MAX);
1063+
dictoffset += (Py_ssize_t)size;
10601064

1061-
dictoffset += (long)size;
10621065
_PyObject_ASSERT(obj, dictoffset > 0);
10631066
_PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
10641067
}

0 commit comments

Comments
 (0)