Skip to content

Commit d5cf0dd

Browse files
committed
pythongh-103987: fix crash in mmap module
1 parent fbf3596 commit d5cf0dd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Lib/test/test_mmap.py

+17
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,23 @@ def test_bad_file_desc(self):
264264
# Try opening a bad file descriptor...
265265
self.assertRaises(OSError, mmap.mmap, -2, 4096)
266266

267+
def test_if_crash(self): # test for issue gh-103987
268+
with open("TESTFN", "w+") as f:
269+
f.write("foobar")
270+
f.flush()
271+
272+
class X(object):
273+
def __index__(self):
274+
m.close()
275+
return 1
276+
277+
m = mmap.mmap(f.fileno(), 6, access=mmap.ACCESS_READ)
278+
self.assertEqual(m[1], 111)
279+
with self.assertRaises(ValueError):
280+
m[X()] # should not crash
281+
282+
m.close()
283+
267284
def test_tougher_find(self):
268285
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
269286
# searching for data with embedded \0 bytes didn't work.

Modules/mmapmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,9 @@ mmap_item(mmap_object *self, Py_ssize_t i)
933933
static PyObject *
934934
mmap_subscript(mmap_object *self, PyObject *item)
935935
{
936-
CHECK_VALID(NULL);
937936
if (PyIndex_Check(item)) {
938937
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
938+
CHECK_VALID(NULL);
939939
if (i == -1 && PyErr_Occurred())
940940
return NULL;
941941
if (i < 0)

0 commit comments

Comments
 (0)