From f9b2aa2efed3374df55fe945922f67a322a5d695 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 15 Nov 2014 13:21:01 +0200 Subject: Issue #22193: Fixed integer overflow error in sys.getsizeof(). Fixed an error in _PySys_GetSizeOf declaration. --- Python/sysmodule.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 5cab14923a..407d770ae9 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -689,7 +689,7 @@ _PySys_GetSizeOf(PyObject *o) { 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 @@ _PySys_GetSizeOf(PyObject *o) 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 * -- cgit v1.2.1