https://github.com/python/cpython/commit/f2cab7b0cf019fcc3112018db5e20c00976f33d4
commit: f2cab7b0cf019fcc3112018db5e20c00976f33d4
branch: main
author: esadomer <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-06-07T16:01:24+03:00
summary:

gh-151021: Fix mmap empty searches past the end (GH-151023)

files:
A Misc/NEWS.d/next/Library/2026-06-06-15-20-54.gh-issue-151021.J4qk2A.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 177fe45e8d9749..2e2ac147968dd4 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -354,6 +354,8 @@ def test_find_end(self):
         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
index 00000000000000..0617fa068c844d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-06-06-15-20-54.gh-issue-151021.J4qk2A.rst
@@ -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.
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index a30afe91f8fa17..6fb04ba7bd47c6 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -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;

_______________________________________________
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]

Reply via email to