Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4774fb

Browse files
committed
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).
1 parent 02ff6a9 commit d4774fb

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

Objects/rangeobject.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ range_item(rangeobject *r, int i)
130130
static int
131131
range_length(rangeobject *r)
132132
{
133-
return r->len;
133+
#if LONG_MAX != INT_MAX
134+
if (r->len > INT_MAX) {
135+
PyErr_SetString(PyExc_ValueError,
136+
"xrange object size cannot be reported");
137+
return -1;
138+
}
139+
#endif
140+
return (int)(r->len);
134141
}
135142

136143
static PyObject *

0 commit comments

Comments
 (0)