Skip to content

Commit 05f1641

Browse files
ZackerySpytzvstinner
authored andcommitted
bpo-26423: Fix possible overflow in wrap_lenfunc() (GH-13606)
Fix possible overflow in wrap_lenfunc() when sizeof(long) < sizeof(Py_ssize_t) (e.g., 64-bit Windows).
1 parent 0453081 commit 05f1641

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/test/test_descr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ def foo(self): return 1
389389
a.setstate(100)
390390
self.assertEqual(a.getstate(), 100)
391391

392+
def test_wrap_lenfunc_bad_cast(self):
393+
self.assertEqual(range(sys.maxsize).__len__(), sys.maxsize)
394+
395+
392396
class ClassPropertiesAndMethods(unittest.TestCase):
393397

394398
def assertHasAttr(self, obj, name):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix possible overflow in ``wrap_lenfunc()`` when
2+
``sizeof(long) < sizeof(Py_ssize_t)`` (e.g., 64-bit Windows).

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5536,7 +5536,7 @@ wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
55365536
res = (*func)(self);
55375537
if (res == -1 && PyErr_Occurred())
55385538
return NULL;
5539-
return PyLong_FromLong((long)res);
5539+
return PyLong_FromSsize_t(res);
55405540
}
55415541

55425542
static PyObject *

0 commit comments

Comments
 (0)