diff options
author | Marc-André Lemburg <mal@egenix.com> | 2001-01-16 11:54:12 (GMT) |
---|---|---|
committer | Marc-André Lemburg <mal@egenix.com> | 2001-01-16 11:54:12 (GMT) |
commit | 3a645e4dd4eebbfbbfad8443558bb3b879e23896 (patch) | |
tree | 97fdb22065892ae35ce0f25bf62fbae5718df32d /Objects/unicodeobject.c | |
parent | 1c5aa6901fb13f8112ead1786868f0f8ed4c9e2d (diff) | |
download | cpython-3a645e4dd4eebbfbbfad8443558bb3b879e23896.zip cpython-3a645e4dd4eebbfbbfad8443558bb3b879e23896.tar.gz cpython-3a645e4dd4eebbfbbfad8443558bb3b879e23896.tar.bz2 |
Added checks to prevent PyUnicode_Count() from dumping core
in case the parameters are out of bounds and fixes error handling
for .count(), .startswith() and .endswith() for the case of
mixed string/Unicode objects.
This patch adds Python style index semantics to PyUnicode_Count()
indices (including the special handling of negative indices).
The patch is an extended version of patch #103249 submitted
by Michael Hudson (mwh) on SF. It also includes new test cases.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 8f7b354..a3678d5 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1976,7 +1976,7 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, x = Py_None; Py_INCREF(x); } else - goto onError; + goto onError; } /* Apply mapping */ @@ -2014,9 +2014,9 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, (targetsize << 2); extrachars += needed; if (_PyUnicode_Resize(v, PyUnicode_GET_SIZE(v) + needed)) { - Py_DECREF(x); - goto onError; - } + Py_DECREF(x); + goto onError; + } p = PyUnicode_AS_UNICODE(v) + oldpos; } Py_UNICODE_COPY(p, @@ -2112,7 +2112,7 @@ PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, x = Py_None; Py_INCREF(x); } else - goto onError; + goto onError; } /* Apply mapping */ @@ -2150,9 +2150,9 @@ PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, (targetsize << 2); extrachars += needed; if (_PyString_Resize(&v, PyString_GET_SIZE(v) + needed)) { - Py_DECREF(x); - goto onError; - } + Py_DECREF(x); + goto onError; + } s = PyString_AS_STRING(v) + oldpos; } memcpy(s, @@ -2392,6 +2392,17 @@ int count(PyUnicodeObject *self, { int count = 0; + if (start < 0) + start += self->length; + if (start < 0) + start = 0; + if (end > self->length) + end = self->length; + if (end < 0) + end += self->length; + if (end < 0) + end = 0; + if (substring->length == 0) return (end - start + 1); |