Skip to content

Commit fc587e8

Browse files
gh-154258: Fix mmap.resize() crash on NetBSD growing a shared anonymous mapping
NetBSD mremap() returns a mapping whose grown region is not backed when growing a shared anonymous mapping, so accessing it crashes. Reject it with ValueError, as is already done on Linux. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 85ab112 commit fc587e8

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,10 @@ def test_resize_up_anonymous_mapping(self):
907907

908908
with mmap.mmap(-1, start_size) as m:
909909
m[:] = data
910-
if sys.platform.startswith(('linux', 'android')):
911-
# Can't expand a shared anonymous mapping on Linux.
912-
# See https://bugzilla.kernel.org/show_bug.cgi?id=8691
910+
if sys.platform.startswith(('linux', 'android', 'netbsd')):
911+
# Can't expand a shared anonymous mapping on Linux
912+
# (see https://bugzilla.kernel.org/show_bug.cgi?id=8691)
913+
# or NetBSD.
913914
with self.assertRaises(ValueError):
914915
m.resize(new_size)
915916
else:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :meth:`mmap.mmap.resize` on NetBSD when growing a shared
2+
anonymous mapping. :meth:`!resize` now raises :exc:`ValueError` in this
3+
case, as it already did on Linux.

Modules/mmapmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,10 +969,13 @@ mmap_mmap_resize_impl(mmap_object *self, Py_ssize_t new_size)
969969
#ifdef UNIX
970970
void *newmap;
971971

972-
#ifdef __linux__
972+
#if defined(__linux__) || defined(__NetBSD__)
973+
// Linux mremap() refuses to grow a shared anonymous mapping, and
974+
// NetBSD mremap() returns a mapping whose grown region is not backed,
975+
// so accessing it crashes. Reject it here in both cases.
973976
if (self->fd == -1 && !(self->flags & MAP_PRIVATE) && new_size > self->size) {
974977
PyErr_Format(PyExc_ValueError,
975-
"mmap: can't expand a shared anonymous mapping on Linux");
978+
"mmap: can't expand a shared anonymous mapping");
976979
return NULL;
977980
}
978981
#endif

0 commit comments

Comments
 (0)