Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c682140

Browse files
committed
Trent Mick:
Fix the string methods that implement slice-like semantics with optional args (count, find, endswith, etc.) to properly handle indeces outside [INT_MIN, INT_MAX]. Previously the "i" formatter for PyArg_ParseTuple was used to get the indices. These could overflow. This patch changes the string methods to use the "O&" formatter with the slice_index() function from ceval.c which is used to do the same job for Python code slices (e.g. 'abcabcabc'[0:1000000000L]). slice_index() is renamed _PyEval_SliceIndex() and is now exported. As well, the return values for success/fail were changed to make slice_index directly usable as required by the "O&" formatter. [GvR: shouldn't a similar patch be applied to unicodeobject.c?]
1 parent 20c6add commit c682140

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

Objects/stringobject.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,8 @@ string_find_internal(self, args, dir)
822822
int n, i = 0, last = INT_MAX;
823823
PyObject *subobj;
824824

825-
if (!PyArg_ParseTuple(args, "O|ii:find/rfind/index/rindex",
826-
&subobj, &i, &last))
825+
if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex",
826+
&subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
827827
return -2;
828828
if (PyString_Check(subobj)) {
829829
sub = PyString_AS_STRING(subobj);
@@ -1194,8 +1194,10 @@ string_count(self, args)
11941194
int m, r;
11951195
PyObject *subobj;
11961196

1197-
if (!PyArg_ParseTuple(args, "O|ii:count", &subobj, &i, &last))
1197+
if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj,
1198+
_PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
11981199
return NULL;
1200+
11991201
if (PyString_Check(subobj)) {
12001202
sub = PyString_AS_STRING(subobj);
12011203
n = PyString_GET_SIZE(subobj);
@@ -1617,7 +1619,8 @@ string_startswith(self, args)
16171619
int end = -1;
16181620
PyObject *subobj;
16191621

1620-
if (!PyArg_ParseTuple(args, "O|ii:startswith", &subobj, &start, &end))
1622+
if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
1623+
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
16211624
return NULL;
16221625
if (PyString_Check(subobj)) {
16231626
prefix = PyString_AS_STRING(subobj);
@@ -1671,7 +1674,8 @@ string_endswith(self, args)
16711674
int lower, upper;
16721675
PyObject *subobj;
16731676

1674-
if (!PyArg_ParseTuple(args, "O|ii:endswith", &subobj, &start, &end))
1677+
if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
1678+
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
16751679
return NULL;
16761680
if (PyString_Check(subobj)) {
16771681
suffix = PyString_AS_STRING(subobj);

0 commit comments

Comments
 (0)