Skip to content

gh-99845: _PySys_GetSizeOf() uses size_t #99903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,15 @@ def setUp(self):

check_sizeof = test.support.check_sizeof

def test_sizeof_method(self):
class Sizeof(int):
def __sizeof__(self):
return int(self)

header_size = self.gc_headsize * 2
self.assertEqual(sys.getsizeof(Sizeof(0)), header_size)
self.assertEqual(sys.getsizeof(Sizeof(100)), 100 + header_size)

def test_gc_head_size(self):
# Check that the gc header size is added to objects tracked by the gc.
vsize = test.support.calcvobjsize
Expand Down Expand Up @@ -1297,17 +1306,20 @@ def __sizeof__(self):
self.assertRaises(TypeError, sys.getsizeof, FloatSizeof())
self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel)

# size_t maximum
header_size = self.gc_headsize * 2
maxsize = (sys.maxsize * 2 + 1) - header_size

class OverflowSizeof(int):
def __sizeof__(self):
return int(self)
self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)),
sys.maxsize + self.gc_headsize*2)

self.assertEqual(sys.getsizeof(OverflowSizeof(maxsize)),
maxsize + header_size)
with self.assertRaises(OverflowError):
sys.getsizeof(OverflowSizeof(maxsize + 1))
with self.assertRaises(OverflowError):
sys.getsizeof(OverflowSizeof(sys.maxsize + 1))
with self.assertRaises(ValueError):
sys.getsizeof(OverflowSizeof(-1))
with self.assertRaises((ValueError, OverflowError)):
sys.getsizeof(OverflowSizeof(-sys.maxsize - 1))

def test_default(self):
size = test.support.calcvobjsize
Expand Down
33 changes: 15 additions & 18 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,44 +1741,41 @@ sys_set_int_max_str_digits_impl(PyObject *module, int maxdigits)
size_t
_PySys_GetSizeOf(PyObject *o)
{
PyObject *res = NULL;
PyObject *method;
Py_ssize_t size;
PyThreadState *tstate = _PyThreadState_GET();

/* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0) {
return (size_t)-1;
}

method = _PyObject_LookupSpecial(o, &_Py_ID(__sizeof__));
PyObject *method = _PyObject_LookupSpecial(o, &_Py_ID(__sizeof__));
PyThreadState *tstate = _PyThreadState_GET();
if (method == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __sizeof__",
Py_TYPE(o)->tp_name);
}
}
else {
res = _PyObject_CallNoArgs(method);
Py_DECREF(method);
return (size_t)-1;
}

if (res == NULL)
PyObject *res = _PyObject_CallNoArgs(method);
Py_DECREF(method);
if (res == NULL) {
return (size_t)-1;
}

size = PyLong_AsSsize_t(res);
size_t size = PyLong_AsSize_t(res);
Py_DECREF(res);
if (size == -1 && _PyErr_Occurred(tstate))
if (size == (size_t)-1 && _PyErr_Occurred(tstate)) {
return (size_t)-1;
}

if (size < 0) {
_PyErr_SetString(tstate, PyExc_ValueError,
"__sizeof__() should return >= 0");
size_t header_size = _PyType_PreHeaderSize(Py_TYPE(o));
if (size > SIZE_MAX - header_size) {
PyErr_SetString(PyExc_OverflowError,
"__sizeof__() greater than size_t maximum");
return (size_t)-1;
}

return (size_t)size + _PyType_PreHeaderSize(Py_TYPE(o));
return header_size + size;
}

static PyObject *
Expand Down