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).
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 *