diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-03-30 23:52:50 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-03-30 23:52:50 (GMT) |
commit | 00ced0f4c7982f1dd181cd69a1e57e408a15edf9 (patch) | |
tree | 0697f8bc096efa2c53ab9c6a8968549f723f41a1 /Objects | |
parent | af2b3f4f3e7a1d4c2e17780ffd1a682ecce2d79e (diff) | |
parent | d455ce4fd4078f1561ec0959dccd5b6561e4d3f6 (diff) | |
download | cpython-00ced0f4c7982f1dd181cd69a1e57e408a15edf9.zip cpython-00ced0f4c7982f1dd181cd69a1e57e408a15edf9.tar.gz cpython-00ced0f4c7982f1dd181cd69a1e57e408a15edf9.tar.bz2 |
merge 3.4
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringlib/transmogrify.h | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/Objects/stringlib/transmogrify.h b/Objects/stringlib/transmogrify.h index dd00976..cae6ea1 100644 --- a/Objects/stringlib/transmogrify.h +++ b/Objects/stringlib/transmogrify.h @@ -15,7 +15,7 @@ stringlib_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) { const char *e, *p; char *q; - size_t i, j; + Py_ssize_t i, j; PyObject *u; static char *kwlist[] = {"tabsize", 0}; int tabsize = 8; @@ -27,35 +27,31 @@ stringlib_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) /* 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) @@ -63,8 +59,8 @@ stringlib_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) 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); @@ -79,8 +75,12 @@ stringlib_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) if (*p == '\n' || *p == '\r') j = 0; } + } return u; + overflow: + PyErr_SetString(PyExc_OverflowError, "result too long"); + return NULL; } Py_LOCAL_INLINE(PyObject *) |