]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Untested code for 64-bit platforms. range_length() is declared as int
authorGuido van Rossum <guido@python.org>
Wed, 11 Sep 2002 15:55:48 +0000 (15:55 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 11 Sep 2002 15:55:48 +0000 (15:55 +0000)
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).

Objects/rangeobject.c

index 3080252ffc6003ffbc1e444ea0e3c78392a9b7e9..6c9daad5597a493f0fe0d1ca939f946a726aa992 100644 (file)
@@ -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 *