summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2001-01-16 11:54:12 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2001-01-16 11:54:12 (GMT)
commit3a645e4dd4eebbfbbfad8443558bb3b879e23896 (patch)
tree97fdb22065892ae35ce0f25bf62fbae5718df32d /Objects/stringobject.c
parent1c5aa6901fb13f8112ead1786868f0f8ed4c9e2d (diff)
downloadcpython-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/stringobject.c')
-rw-r--r--Objects/stringobject.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 091ede7..eed4687 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1236,9 +1236,14 @@ string_count(PyStringObject *self, PyObject *args)
sub = PyString_AS_STRING(subobj);
n = PyString_GET_SIZE(subobj);
}
- else if (PyUnicode_Check(subobj))
- return PyInt_FromLong(
- PyUnicode_Count((PyObject *)self, subobj, i, last));
+ else if (PyUnicode_Check(subobj)) {
+ int count;
+ count = PyUnicode_Count((PyObject *)self, subobj, i, last);
+ if (count == -1)
+ return NULL;
+ else
+ return PyInt_FromLong((long) count);
+ }
else if (PyObject_AsCharBuffer(subobj, &sub, &n))
return NULL;
@@ -1637,10 +1642,15 @@ string_startswith(PyStringObject *self, PyObject *args)
prefix = PyString_AS_STRING(subobj);
plen = PyString_GET_SIZE(subobj);
}
- else if (PyUnicode_Check(subobj))
- return PyInt_FromLong(
- PyUnicode_Tailmatch((PyObject *)self,
- subobj, start, end, -1));
+ else if (PyUnicode_Check(subobj)) {
+ int rc;
+ rc = PyUnicode_Tailmatch((PyObject *)self,
+ subobj, start, end, -1);
+ if (rc == -1)
+ return NULL;
+ else
+ return PyInt_FromLong((long) rc);
+ }
else if (PyObject_AsCharBuffer(subobj, &prefix, &plen))
return NULL;
@@ -1690,10 +1700,15 @@ string_endswith(PyStringObject *self, PyObject *args)
suffix = PyString_AS_STRING(subobj);
slen = PyString_GET_SIZE(subobj);
}
- else if (PyUnicode_Check(subobj))
- return PyInt_FromLong(
- PyUnicode_Tailmatch((PyObject *)self,
- subobj, start, end, +1));
+ else if (PyUnicode_Check(subobj)) {
+ int rc;
+ rc = PyUnicode_Tailmatch((PyObject *)self,
+ subobj, start, end, +1);
+ if (rc == -1)
+ return NULL;
+ else
+ return PyInt_FromLong((long) rc);
+ }
else if (PyObject_AsCharBuffer(subobj, &suffix, &slen))
return NULL;