diff options
| author | Neal Norwitz <nnorwitz@gmail.com> | 2007-06-11 04:32:41 (GMT) |
|---|---|---|
| committer | Neal Norwitz <nnorwitz@gmail.com> | 2007-06-11 04:32:41 (GMT) |
| commit | 8355dd58061649b64b253d6fdff3fd67ac3a4f49 (patch) | |
| tree | 3b7e7d2a12d2e6431e8fb9bd6138b08d391fd5d0 | |
| parent | 11c5275c6129c2600a29111c9529d1e51aaf3cb6 (diff) | |
| download | cpython-8355dd58061649b64b253d6fdff3fd67ac3a4f49.zip cpython-8355dd58061649b64b253d6fdff3fd67ac3a4f49.tar.gz cpython-8355dd58061649b64b253d6fdff3fd67ac3a4f49.tar.bz2 | |
Backport 55874:
Fix a bug when there was a newline in the string expandtabs was called on.
This also catches another condition that can overflow.
| -rw-r--r-- | Lib/test/string_tests.py | 5 | ||||
| -rw-r--r-- | Objects/stringobject.c | 10 | ||||
| -rw-r--r-- | Objects/unicodeobject.c | 10 |
3 files changed, 21 insertions, 4 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index b257d57..69a2225 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -247,8 +247,13 @@ class CommonTest(unittest.TestCase): self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs') self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4) + self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1) self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42) + # This test is only valid when sizeof(int) == sizeof(void*) == 4. + if sys.maxint < (1 << 32) and struct.calcsize('P') == 4: + self.checkraises(OverflowError, + '\ta\n\tb', 'expandtabs', sys.maxint) def test_split(self): self.checkequal(['this', 'is', 'the', 'split', 'function'], diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 8b54643..cee78a0 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3313,7 +3313,8 @@ string_expandtabs(PyStringObject *self, PyObject *args) if (tabsize > 0) { j += tabsize - (j % tabsize); if (old_j > j) { - PyErr_SetString(PyExc_OverflowError, "new string is too long"); + PyErr_SetString(PyExc_OverflowError, + "new string is too long"); return NULL; } old_j = j; @@ -3323,7 +3324,12 @@ string_expandtabs(PyStringObject *self, PyObject *args) j++; if (*p == '\n' || *p == '\r') { i += j; - j = 0; + old_j = j = 0; + if (i < 0) { + PyErr_SetString(PyExc_OverflowError, + "new string is too long"); + return NULL; + } } } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 0640da8..742db6f 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -5701,7 +5701,8 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args) if (tabsize > 0) { j += tabsize - (j % tabsize); if (old_j > j) { - PyErr_SetString(PyExc_OverflowError, "new string is too long"); + PyErr_SetString(PyExc_OverflowError, + "new string is too long"); return NULL; } old_j = j; @@ -5711,7 +5712,12 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args) j++; if (*p == '\n' || *p == '\r') { i += j; - j = 0; + old_j = j = 0; + if (i < 0) { + PyErr_SetString(PyExc_OverflowError, + "new string is too long"); + return NULL; + } } } |
