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

This commit is contained in:
esadomer 2026-06-07 16:01:24 +03:00 committed by GitHub
parent 0f7dc2fefa
commit f2cab7b0cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 5 additions and 2 deletions

View file

@ -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):

View file

@ -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.

View 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;