From: Guido van Rossum Date: Wed, 11 Sep 2002 15:55:48 +0000 (+0000) Subject: Untested code for 64-bit platforms. range_length() is declared as int X-Git-Tag: v2.3c1~4134 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d4774fb6efde8facccfe0b60a37e3ecd4a943bd9;p=thirdparty%2FPython%2Fcpython.git Untested code for 64-bit platforms. range_length() is declared as int but returns r->len which is a long. This doesn't even cause a warning on 32-bit platforms, but can return bogus values on 64-bit platforms (and should cause a compiler warning). Fix this by inserting a range check when LONG_MAX != INT_MAX, and adding an explicit cast to (int) when the test passes. When r->len is out of range, PySequence_Size() and hence len() will report an error (but an iterator will still work). --- diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 3080252ffc60..6c9daad5597a 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -130,7 +130,14 @@ range_item(rangeobject *r, int i) static int range_length(rangeobject *r) { - return r->len; +#if LONG_MAX != INT_MAX + if (r->len > INT_MAX) { + PyErr_SetString(PyExc_ValueError, + "xrange object size cannot be reported"); + return -1; + } +#endif + return (int)(r->len); } static PyObject *