Skip to content

mmap.find() and rfind() match an empty subsequence past the end #151021

Closed

Description

@esadomer

mmap.find() and mmap.rfind() return the mapping length when searching for an empty subsequence with start > len(mapping).

import mmap

with mmap.mmap(-1, 3) as m:
    m[:] = b"abc"
    print(m.find(b"", 4))
    print(m.rfind(b"", 4))

Actual output:

3
3

Expected output:

-1
-1

bytes, bytearray, and str all return -1 for the equivalent operation.

The mmap documentation states that start and end are interpreted as in slice notation, and the existing mmap tests compare search behavior against bytes.find().

The cause appears to be that mmap_gfind_lock_held() clamps a positive start greater than the mapping size to the mapping size. This converts an out-of-range search into a successful empty-subsequence match.

Confirmed on CPython main (884ac3e3ec0), Python 3.12.8, and Python 3.10.0 on Windows.

Linked PRs

Activity

  1. added
    type-bug
    extension-modules
    on Jun 6, 2026
  2. added a commit that references this issue on Jun 7, 2026
  3. serhiy-storchaka commented on Jun 7, 2026

    @serhiy-storchaka
    Member

    Thank you for your contribution, @esadomer. Since this behavior was there from the very beginning, and it isn't look like such little discrepancy can cause serious issues, I think that it is safer to not change it in already released Python versions.

  4. added
    3.15
    3.16
    on Jun 7, 2026
  5. added a commit that references this issue on Jun 7, 2026
  6. added a commit that references this issue on Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.153.16extension-modulestype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions