Skip to content

Commit

Permalink
pythongh-103987: fix crash in mmap module
Browse files Browse the repository at this point in the history
  • Loading branch information
Agent-Hellboy committed Apr 29, 2023
1 parent fbf3596 commit d5cf0dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,23 @@ def test_bad_file_desc(self):
# Try opening a bad file descriptor...
self.assertRaises(OSError, mmap.mmap, -2, 4096)

def test_if_crash(self): # test for issue gh-103987
with open("TESTFN", "w+") as f:
f.write("foobar")
f.flush()

class X(object):
def __index__(self):
m.close()
return 1

m = mmap.mmap(f.fileno(), 6, access=mmap.ACCESS_READ)
self.assertEqual(m[1], 111)
with self.assertRaises(ValueError):
m[X()] # should not crash

m.close()

def test_tougher_find(self):
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
# searching for data with embedded \0 bytes didn't work.
Expand Down
2 changes: 1 addition & 1 deletion Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,9 @@ mmap_item(mmap_object *self, Py_ssize_t i)
static PyObject *
mmap_subscript(mmap_object *self, PyObject *item)
{
CHECK_VALID(NULL);

This comment has been minimized.

Copy link
@sunmy2019

sunmy2019 Apr 29, 2023

You cannot delete it here. As other code paths will not be protected.

Keep this line as is.

I suggest adding CHECK_VALID(NULL); right before each read assess of self->data.

if (PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
CHECK_VALID(NULL);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
Expand Down

0 comments on commit d5cf0dd

Please sign in to comment.