Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80d3610

Browse files
committed
Merge fix for issue #11391
2 parents d1b1991 + 7b50c2c commit 80d3610

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 8 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Core and Builtins
3131
Library
3232
-------
3333

34+
- Issue #11391: Writing to a mmap object created with
35+
``mmap.PROT_READ|mmap.PROT_EXEC`` would segfault instead of raising a
36+
TypeError. Patch by Charles-François Natali.
37+
3438
- Issue #11306: mailbox in certain cases adapts to an inability to open
3539
certain files in read-write mode. Previously it detected this by
3640
checking for EACCES, now it also checks for EROFS.

Modules/mmapmodule.c

Lines changed: 10 additions & 5 deletions
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)