Skip to content

Commit f874deb

Browse files
committed
Merge fix for issue #11391
2 parents 53b13e9 + 80d3610 commit f874deb

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

Lib/test/test_mmap.py

+8
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def test_access_parameter(self):
234234
flags=mmap.MAP_PRIVATE,
235235
prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
236236

237+
# Try writing with PROT_EXEC and without PROT_WRITE
238+
prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0)
239+
with open(TESTFN, "r+b") as f:
240+
m = mmap.mmap(f.fileno(), mapsize, prot=prot)
241+
self.assertRaises(TypeError, m.write, b"abcdef")
242+
self.assertRaises(TypeError, m.write_byte, 0)
243+
m.close()
244+
237245
def test_bad_file_desc(self):
238246
# Try opening a bad file descriptor...
239247
self.assertRaises(mmap.error, mmap.mmap, -2, 4096)

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Core and Builtins
5555
Library
5656
-------
5757

58+
- Issue #11391: Writing to a mmap object created with
59+
``mmap.PROT_READ|mmap.PROT_EXEC`` would segfault instead of raising a
60+
TypeError. Patch by Charles-François Natali.
61+
5862
- Issue #9795: add context manager protocol support for nntplib.NNTP class.
5963

6064
- Issue #11306: mailbox in certain cases adapts to an inability to open

Modules/mmapmodule.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -1106,17 +1106,22 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11061106
prot = PROT_READ | PROT_WRITE;
11071107
break;
11081108
case ACCESS_DEFAULT:
1109-
/* use the specified or default values of flags and prot */
1109+
/* map prot to access type */
1110+
if ((prot & PROT_READ) && (prot & PROT_WRITE)) {
1111+
/* ACCESS_DEFAULT */
1112+
}
1113+
else if (prot & PROT_WRITE) {
1114+
access = ACCESS_WRITE;
1115+
}
1116+
else {
1117+
access = ACCESS_READ;
1118+
}
11101119
break;
11111120
default:
11121121
return PyErr_Format(PyExc_ValueError,
11131122
"mmap invalid access parameter.");
11141123
}
11151124

1116-
if (prot == PROT_READ) {
1117-
access = ACCESS_READ;
1118-
}
1119-
11201125
#ifdef HAVE_FSTAT
11211126
# ifdef __VMS
11221127
/* on OpenVMS we must ensure that all bytes are written to the file */

0 commit comments

Comments
 (0)