Skip to content

Commit

Permalink
bpo-26423: Fix possible overflow in wrap_lenfunc() (GH-13606) (GH-13625)
Browse files Browse the repository at this point in the history
Fix possible overflow in wrap_lenfunc() when
sizeof(long) < sizeof(Py_ssize_t) (e.g., 64-bit Windows).

(cherry picked from commit 05f1641)
  • Loading branch information
vstinner committed May 28, 2019
1 parent d9d1045 commit 80dfe99
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ def foo(self): return 1
a.setstate(100)
self.assertEqual(a.getstate(), 100)

def test_wrap_lenfunc_bad_cast(self):
self.assertEqual(xrange(sys.maxsize).__len__(), sys.maxsize)


class ClassPropertiesAndMethods(unittest.TestCase):

def assertHasAttr(self, obj, name):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix possible overflow in ``wrap_lenfunc()`` when
``sizeof(long) < sizeof(Py_ssize_t)`` (e.g., 64-bit Windows).
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4398,7 +4398,7 @@ wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
res = (*func)(self);
if (res == -1 && PyErr_Occurred())
return NULL;
return PyInt_FromLong((long)res);
return PyInt_FromSsize_t(res);
}

static PyObject *
Expand Down

0 comments on commit 80dfe99

Please sign in to comment.