Skip to content

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

@esadomer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions