Issue #22193: Fixed integer overflow error in sys.getsizeof().
Fixed an error in _PySys_GetSizeOf declaration.
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 5cab149..407d770 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -689,7 +689,7 @@
{
static PyObject *str__sizeof__ = NULL;
PyObject *res = NULL;
- size_t size;
+ Py_ssize_t size;
/* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0)
@@ -718,14 +718,19 @@
size = (size_t)PyInt_AsSsize_t(res);
Py_DECREF(res);
- if (size == (size_t)-1 && PyErr_Occurred())
+ if (size == -1 && PyErr_Occurred())
return (size_t)-1;
}
+ if (size < 0) {
+ PyErr_SetString(PyExc_ValueError, "__sizeof__() should return >= 0");
+ return (size_t)-1;
+ }
+
/* add gc_head size */
if (PyObject_IS_GC(o))
- size += sizeof(PyGC_Head);
- return size;
+ return ((size_t)size) + sizeof(PyGC_Head);
+ return (size_t)size;
}
static PyObject *