Skip to content

[mypyc] Clamp huge start and end indices in str.find, rfind and count - #22433

Merged
JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-find-clamp
Nov 12, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-str-find-clamp

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

str.find(), str.rfind() and str.count() raised OverflowError for start or end indices that don't fit in Py_ssize_t. CPython clamps them instead, since they are out of range for any string:

"abcab".find("b", 0, 2**100)  # CPython: 1; compiled: OverflowError
"abcab".count("b", -2**100)   # CPython: 2; compiled: OverflowError

Clamp them the same way as CPython. Indices that fit in a short int still take the existing fast path.

Start and end indices that don't fit in Py_ssize_t raised OverflowError.
CPython clamps them instead, since they are out of range for any string.
@Jahongir-Qurbonov Jahongir-Qurbonov mentioned this pull request Nov 10, 2026
13 of 14 tasks

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@JukkaL
JukkaL merged commit ef1c0b8 into python:master Nov 12, 2026
18 checks passed
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-str-find-clamp branch November 14, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants