]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-151021: Fix mmap empty searches past the end (GH-151023)
authoresadomer <54475303+esadomer@users.noreply.github.com>
Sun, 7 Jun 2026 13:01:24 +0000 (16:01 +0300)
committerGitHub <noreply@github.com>
Sun, 7 Jun 2026 13:01:24 +0000 (16:01 +0300)
Lib/test/test_mmap.py
Misc/NEWS.d/next/Library/2026-06-06-15-20-54.gh-issue-151021.J4qk2A.rst [new file with mode: 0644]
Modules/mmapmodule.c

index 177fe45e8d9749063601f1c53e7798e06a12adfb..2e2ac147968dd4a0bd0ad2fb57696919a0891a82 100644 (file)
@@ -354,6 +354,8 @@ class MmapTests(unittest.TestCase):
         self.assertEqual(m.find(b'one', 1, -1), 8)
         self.assertEqual(m.find(b'one', 1, -2), -1)
         self.assertEqual(m.find(bytearray(b'one')), 0)
+        self.assertEqual(m.find(b'', n + 1), -1)
+        self.assertEqual(m.rfind(b'', n + 1), -1)
 
         for i in range(-n-1, n+1):
             for j in range(-n-1, n+1):
diff --git a/Misc/NEWS.d/next/Library/2026-06-06-15-20-54.gh-issue-151021.J4qk2A.rst b/Misc/NEWS.d/next/Library/2026-06-06-15-20-54.gh-issue-151021.J4qk2A.rst
new file mode 100644 (file)
index 0000000..0617fa0
--- /dev/null
@@ -0,0 +1,3 @@
+Fix :meth:`mmap.mmap.find` and :meth:`~mmap.mmap.rfind` to return ``-1``
+when searching for an empty subsequence with a start position past the end
+of the mapping.
index a30afe91f8fa171214058ca3609cebc5c85bfaa8..6fb04ba7bd47c67dfbe5f540eeb59a6fe9dcdc8b 100644 (file)
@@ -620,8 +620,6 @@ mmap_gfind_lock_held(mmap_object *self, Py_buffer *view, PyObject *start_obj,
         start += self->size;
     if (start < 0)
         start = 0;
-    else if (start > self->size)
-        start = self->size;
 
     if (end < 0)
         end += self->size;