diff options
author | Guido van Rossum <guido@python.org> | 2000-05-08 14:08:05 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2000-05-08 14:08:05 (GMT) |
commit | c682140de7656db69b9d395c1a80f1b429ef0ae7 (patch) | |
tree | 70d5257b81111a2ef6de94f5760cbe7925b7f6d3 /Objects/stringobject.c | |
parent | 20c6add7ff268340797cc8a038a79417e98a7a8c (diff) | |
download | cpython-c682140de7656db69b9d395c1a80f1b429ef0ae7.zip cpython-c682140de7656db69b9d395c1a80f1b429ef0ae7.tar.gz cpython-c682140de7656db69b9d395c1a80f1b429ef0ae7.tar.bz2 |
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?]
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index c94ee88..f17fbf1 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -822,8 +822,8 @@ string_find_internal(self, args, dir) int n, i = 0, last = INT_MAX; PyObject *subobj; - if (!PyArg_ParseTuple(args, "O|ii:find/rfind/index/rindex", - &subobj, &i, &last)) + if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", + &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) return -2; if (PyString_Check(subobj)) { sub = PyString_AS_STRING(subobj); @@ -1194,8 +1194,10 @@ string_count(self, args) int m, r; PyObject *subobj; - if (!PyArg_ParseTuple(args, "O|ii:count", &subobj, &i, &last)) + if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, + _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) return NULL; + if (PyString_Check(subobj)) { sub = PyString_AS_STRING(subobj); n = PyString_GET_SIZE(subobj); @@ -1617,7 +1619,8 @@ string_startswith(self, args) int end = -1; PyObject *subobj; - if (!PyArg_ParseTuple(args, "O|ii:startswith", &subobj, &start, &end)) + if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, + _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) return NULL; if (PyString_Check(subobj)) { prefix = PyString_AS_STRING(subobj); @@ -1671,7 +1674,8 @@ string_endswith(self, args) int lower, upper; PyObject *subobj; - if (!PyArg_ParseTuple(args, "O|ii:endswith", &subobj, &start, &end)) + if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, + _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) return NULL; if (PyString_Check(subobj)) { suffix = PyString_AS_STRING(subobj); |