summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2007-06-09 04:06:30 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2007-06-09 04:06:30 (GMT)
commit66e64e2b6a837bcd95c29a2173d065fdc91f0581 (patch)
tree9d5574f336188233cbac23d8e079ac16d238b647 /Objects/stringobject.c
parent11c58c4c8d8376565dbb8f04d08d915022ba5028 (diff)
downloadcpython-66e64e2b6a837bcd95c29a2173d065fdc91f0581.zip
cpython-66e64e2b6a837bcd95c29a2173d065fdc91f0581.tar.gz
cpython-66e64e2b6a837bcd95c29a2173d065fdc91f0581.tar.bz2
Prevent expandtabs() on string and unicode objects from causing a segfault when
a large width is passed on 32-bit platforms. Found by Google. It would be good for people to review this especially carefully and verify I don't have an off by one error and there is no other way to cause overflow.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 5ae2ca0..8b54643 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -3298,7 +3298,7 @@ string_expandtabs(PyStringObject *self, PyObject *args)
{
const char *e, *p;
char *q;
- Py_ssize_t i, j;
+ Py_ssize_t i, j, old_j;
PyObject *u;
int tabsize = 8;
@@ -3306,12 +3306,18 @@ string_expandtabs(PyStringObject *self, PyObject *args)
return NULL;
/* First pass: determine size of output string */
- i = j = 0;
+ i = j = old_j = 0;
e = PyString_AS_STRING(self) + PyString_GET_SIZE(self);
for (p = PyString_AS_STRING(self); p < e; p++)
if (*p == '\t') {
- if (tabsize > 0)
+ if (tabsize > 0) {
j += tabsize - (j % tabsize);
+ if (old_j > j) {
+ PyErr_SetString(PyExc_OverflowError, "new string is too long");
+ return NULL;
+ }
+ old_j = j;
+ }
}
else {
j++;
@@ -3321,6 +3327,11 @@ string_expandtabs(PyStringObject *self, PyObject *args)
}
}
+ if ((i + j) < 0) {
+ PyErr_SetString(PyExc_OverflowError, "new string is too long");
+ return NULL;
+ }
+
/* Second pass: create output string and fill it */
u = PyString_FromStringAndSize(NULL, i + j);
if (!u)