[3.14] gh-154258: Fix mmap.resize() crash on NetBSD growing a shared anonymous mapping (GH-154259) (GH-154263)

serhiy-storchaka <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/71f7a04f9aac4db44b04b9ac33f704f0eaf6eb18
commit: 71f7a04f9aac4db44b04b9ac33f704f0eaf6eb18
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-20T14:34:24Z
summary:

[3.14] gh-154258: Fix mmap.resize() crash on NetBSD growing a shared anonymous mapping (GH-154259) (GH-154263)

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.
(cherry picked from commit 542b98293108a84fa5d904fb6dcf8bfb5080ec93)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-20-18-30-00.gh-issue-154258.mmapresize.rst
M Lib/test/test_mmap.py
M Modules/mmapmodule.c

diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index f468dda1d163c4..93be6c2830357e 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -919,9 +919,10 @@ def test_resize_up_anonymous_mapping(self):
 
         with mmap.mmap(-1, start_size) as m:
             m[:] = data
-            if sys.platform.startswith(('linux', 'android')):
-                # Can't expand a shared anonymous mapping on Linux.
-                # See https://bugzilla.kernel.org/show_bug.cgi?id=8691
+            if sys.platform.startswith(('linux', 'android', 'netbsd')):
+                # Can't expand a shared anonymous mapping on Linux
+                # (see https://bugzilla.kernel.org/show_bug.cgi?id=8691)
+                # or NetBSD.
                 with self.assertRaises(ValueError):
                     m.resize(new_size)
             else:
diff --git a/Misc/NEWS.d/next/Library/2026-07-20-18-30-00.gh-issue-154258.mmapresize.rst b/Misc/NEWS.d/next/Library/2026-07-20-18-30-00.gh-issue-154258.mmapresize.rst
new file mode 100644
index 00000000000000..4c33336e0bbdca
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-20-18-30-00.gh-issue-154258.mmapresize.rst
@@ -0,0 +1,3 @@
+Fix a crash in :meth:`mmap.mmap.resize` on NetBSD when growing a shared
+anonymous mapping.  :meth:`!resize` now raises :exc:`ValueError` in this
+case, as it already did on Linux.
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 43f4763c4fdc0d..f7b8ab336fed73 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -969,10 +969,13 @@ mmap_mmap_resize_impl(mmap_object *self, Py_ssize_t new_size)
 #else
         void *newmap;
 
-#ifdef __linux__
+#if defined(__linux__) || defined(__NetBSD__)
+        // Linux mremap() refuses to grow a shared anonymous mapping, and
+        // NetBSD mremap() returns a mapping whose grown region is not backed,
+        // so accessing it crashes.  Reject it here in both cases.
         if (self->fd == -1 && !(self->flags & MAP_PRIVATE) && new_size > self->size) {
             PyErr_Format(PyExc_ValueError,
-                "mmap: can't expand a shared anonymous mapping on Linux");
+                "mmap: can't expand a shared anonymous mapping");
             return NULL;
         }
 #endif

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.