summaryrefslogtreecommitdiffstats
path: root/Objects/stringlib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-03-30 23:52:22 (GMT)
committerBenjamin Peterson <benjamin@python.org>2014-03-30 23:52:22 (GMT)
commit0ad6098b67acd0b00754e0b6668cb93213cb32d2 (patch)
treeafbb804a94bc31b0e2d90ab9047efbad916c56ab /Objects/stringlib
parent3e952d56ea1c50294cf3445dac5dc6a8ad27fbbe (diff)
parent23cf403ca17dc1ac2eeccf7476a1ffd47ea88137 (diff)
downloadcpython-0ad6098b67acd0b00754e0b6668cb93213cb32d2.zip
cpython-0ad6098b67acd0b00754e0b6668cb93213cb32d2.tar.gz
cpython-0ad6098b67acd0b00754e0b6668cb93213cb32d2.tar.bz2
merge 3.2
Diffstat (limited to 'Objects/stringlib')
-rw-r--r--Objects/stringlib/transmogrify.h38
1 files changed, 19 insertions, 19 deletions
diff --git a/Objects/stringlib/transmogrify.h b/Objects/stringlib/transmogrify.h
index 90fa129..cbd7144 100644
--- a/Objects/stringlib/transmogrify.h
+++ b/Objects/stringlib/transmogrify.h
@@ -15,7 +15,7 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
{
const char *e, *p;
char *q;
- size_t i, j;
+ Py_ssize_t i, j;
PyObject *u;
int tabsize = 8;
@@ -25,35 +25,31 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
/* First pass: determine size of output string */
i = j = 0;
e = STRINGLIB_STR(self) + STRINGLIB_LEN(self);
- for (p = STRINGLIB_STR(self); p < e; p++)
+ for (p = STRINGLIB_STR(self); p < e; p++) {
if (*p == '\t') {
if (tabsize > 0) {
- j += tabsize - (j % tabsize);
- if (j > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "result is too long");
- return NULL;
- }
+ Py_ssize_t incr = tabsize - (j % tabsize);
+ if (j > PY_SSIZE_T_MAX - incr)
+ goto overflow;
+ j += incr;
}
}
else {
+ if (j > PY_SSIZE_T_MAX - 1)
+ goto overflow;
j++;
if (*p == '\n' || *p == '\r') {
+ if (i > PY_SSIZE_T_MAX - j)
+ goto overflow;
i += j;
j = 0;
- if (i > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "result is too long");
- return NULL;
- }
}
}
-
- if ((i + j) > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError, "result is too long");
- return NULL;
}
+ if (i > PY_SSIZE_T_MAX - j)
+ goto overflow;
+
/* Second pass: create output string and fill it */
u = STRINGLIB_NEW(NULL, i + j);
if (!u)
@@ -61,8 +57,8 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
j = 0;
q = STRINGLIB_STR(u);
-
- for (p = STRINGLIB_STR(self); p < e; p++)
+
+ for (p = STRINGLIB_STR(self); p < e; p++) {
if (*p == '\t') {
if (tabsize > 0) {
i = tabsize - (j % tabsize);
@@ -77,8 +73,12 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
if (*p == '\n' || *p == '\r')
j = 0;
}
+ }
return u;
+ overflow:
+ PyErr_SetString(PyExc_OverflowError, "result too long");
+ return NULL;
}
Py_LOCAL_INLINE(PyObject *)