summaryrefslogtreecommitdiff
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-11-15 13:21:37 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2014-11-15 13:21:37 +0200
commit5da37a6cb2b66e25844e3ee0b410fcdc5f2da4a6 (patch)
tree938372d562060b6f1d451213075925ba0d8abd83 /Python/sysmodule.c
parente5815b0fd217523e90023d6521a2261413ca5cba (diff)
downloadcpython-5da37a6cb2b66e25844e3ee0b410fcdc5f2da4a6.tar.gz
Issue #22193: Fixed integer overflow error in sys.getsizeof().
Fixed an error in _PySys_GetSizeOf declaration.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 39fe53fb7e..106fc84fa9 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -868,7 +868,7 @@ _PySys_GetSizeOf(PyObject *o)
{
PyObject *res = NULL;
PyObject *method;
- size_t size;
+ Py_ssize_t size;
/* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0)
@@ -889,15 +889,20 @@ _PySys_GetSizeOf(PyObject *o)
if (res == NULL)
return (size_t)-1;
- size = PyLong_AsSize_t(res);
+ size = PyLong_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 *