From 261614e46d1acbf849959301a7755c3dacb96ecd Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Tue, 10 Nov 2026 22:50:52 +0500 Subject: [PATCH] [mypyc] Clamp huge start and end indices in str.find, rfind and count 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. --- mypyc/lib-rt/str_ops.c | 46 +++++++++++++++----------------- mypyc/test-data/run-strings.test | 25 +++++++++++++++++ 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index e5d284314b3f..2930d22daf23 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -236,22 +236,30 @@ PyObject *CPyStr_Build(Py_ssize_t len, ...) { return res; } +// Convert a start or end index to Py_ssize_t. Like CPython, clamp values that don't fit +// in Py_ssize_t (these are out of range for any string) instead of raising an error. +static Py_ssize_t CPyStr_AsSliceIndex(CPyTagged x) { + if (likely(CPyTagged_CheckShort(x))) { + return CPyTagged_ShortAsSsize_t(x); + } + PyObject *obj = CPyTagged_LongAsObject(x); + Py_ssize_t n = PyLong_AsSsize_t(obj); + if (n == -1 && PyErr_Occurred()) { + // The only possible error is an overflow + PyErr_Clear(); + return PyLong_IsNegative(obj) ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX; + } + return n; +} + CPyTagged CPyStr_Find(PyObject *str, PyObject *substr, CPyTagged start, int direction) { CPyTagged end = PyUnicode_GET_LENGTH(str) << 1; return CPyStr_FindWithEnd(str, substr, start, end, direction); } CPyTagged CPyStr_FindWithEnd(PyObject *str, PyObject *substr, CPyTagged start, CPyTagged end, int direction) { - Py_ssize_t temp_start = CPyTagged_AsSsize_t(start); - if (temp_start == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return CPY_INT_TAG; - } - Py_ssize_t temp_end = CPyTagged_AsSsize_t(end); - if (temp_end == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return CPY_INT_TAG; - } + Py_ssize_t temp_start = CPyStr_AsSliceIndex(start); + Py_ssize_t temp_end = CPyStr_AsSliceIndex(end); Py_ssize_t index = PyUnicode_Find(str, substr, temp_start, temp_end, direction); if (unlikely(index == -2)) { return CPY_INT_TAG; @@ -605,26 +613,14 @@ PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors) { } Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start) { - Py_ssize_t temp_start = CPyTagged_AsSsize_t(start); - if (temp_start == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return -1; - } + Py_ssize_t temp_start = CPyStr_AsSliceIndex(start); Py_ssize_t end = PyUnicode_GET_LENGTH(unicode); return PyUnicode_Count(unicode, substring, temp_start, end); } Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end) { - Py_ssize_t temp_start = CPyTagged_AsSsize_t(start); - if (temp_start == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return -1; - } - Py_ssize_t temp_end = CPyTagged_AsSsize_t(end); - if (temp_end == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); - return -1; - } + Py_ssize_t temp_start = CPyStr_AsSliceIndex(start); + Py_ssize_t temp_end = CPyStr_AsSliceIndex(end); return PyUnicode_Count(unicode, substring, temp_start, temp_end); } diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index 4b75aa695fa5..a9a0719b2920 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -324,6 +324,21 @@ def test_find() -> None: assert rfind(s, "b", 3, 5) == 4 assert rfind(s, "b", 3, 4) == -1 +def test_find_huge_indices() -> None: + # Indices that don't fit in Py_ssize_t are clamped, like in CPython + s = "abcab" + assert find(s, "b", 2**62) == -1 + assert find(s, "b", 2**63) == -1 + assert find(s, "b", -2**63 - 1) == 1 + assert find(s, "b", 0, 2**100) == 1 + assert find(s, "b", 0, -2**100) == -1 + assert find(s, "", -2**100) == 0 + assert find("", "", 2**63) == -1 + assert rfind(s, "b", -2**100) == 4 + assert rfind(s, "b", 0, 2**100) == 4 + assert rfind(s, "", 0, 2**100) == 5 + assert rfind(s, "b", 2**100, -2**100) == -1 + def str_to_int(s: str, base: Optional[int] = None) -> int: if base: return int(s, base) @@ -1214,6 +1229,16 @@ def test_count_multi_start_end_emoji() -> None: assert string.count("😴😴😴", 0, 12) == 1, string.count("😴😴😴", 0, 12) assert string.count("🚀🚀🚀", 0, 12) == 2, string.count("🚀🚀🚀", 0, 12) assert string.count("ñññ", 0, 12) == 1, string.count("ñññ", 0, 12) +def test_count_huge_indices() -> None: + # Indices that don't fit in Py_ssize_t are clamped, like in CPython + string = "abcbcb" + assert string.count("b", 2**62) == 0 + assert string.count("b", 2**63) == 0 + assert string.count("b", -2**100) == 3 + assert string.count("b", 0, 2**100) == 3 + assert string.count("b", -2**100, -2**100) == 0 + assert string.count("", 2**62) == 0 + assert string.count("", -2**100, 2**100) == 7 [case testIsInstance] from copysubclass import subc