]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154258: Fix mmap.resize() crash on NetBSD growing a shared anonymous mapping ...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Jul 2026 14:06:19 +0000 (17:06 +0300)
committerGitHub <noreply@github.com>
Mon, 20 Jul 2026 14:06:19 +0000 (14:06 +0000)
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>
Lib/test/test_mmap.py
Misc/NEWS.d/next/Library/2026-07-20-18-30-00.gh-issue-154258.mmapresize.rst [new file with mode: 0644]
Modules/mmapmodule.c

index 2e2ac147968dd4a0bd0ad2fb57696919a0891a82..5be9dfac2f5a76feb87efe2f0edad19c423d7d5d 100644 (file)
@@ -907,9 +907,10 @@ class MmapTests(unittest.TestCase):
 
         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 (file)
index 0000000..4c33336
--- /dev/null
@@ -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.
index d55ab3d4d56edb02a18e54c7ebd0e78649d4568e..e521e95d4ded73fb2d36b02e4e60b8f153ff5e74 100644 (file)
@@ -969,10 +969,13 @@ mmap_mmap_resize_impl(mmap_object *self, Py_ssize_t new_size)
 #ifdef UNIX
         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