diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-11-15 11:21:01 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-11-15 11:21:01 (GMT) |
commit | f9b2aa2efed3374df55fe945922f67a322a5d695 (patch) | |
tree | 9a2a7ec475afa1c6b884e712a42e23c61d62bab6 /Python/sysmodule.c | |
parent | af627902aaf879cf64bf6b4ebd73f4dcf74e29db (diff) | |
download | cpython-f9b2aa2efed3374df55fe945922f67a322a5d695.zip cpython-f9b2aa2efed3374df55fe945922f67a322a5d695.tar.gz cpython-f9b2aa2efed3374df55fe945922f67a322a5d695.tar.bz2 |
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.c | 13 |
1 files changed, 9 insertions, 4 deletions
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 @@ _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 * |