diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2007-06-11 02:16:10 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2007-06-11 02:16:10 (GMT) |
commit | 5c9a81a3d828e775a1500bb8d84dc1dab59513ad (patch) | |
tree | 868a2214eae0f4ac5f30719a835a63d730fcfff8 | |
parent | ba965deea89299611454e43be005df357b75b11d (diff) | |
download | cpython-5c9a81a3d828e775a1500bb8d84dc1dab59513ad.zip cpython-5c9a81a3d828e775a1500bb8d84dc1dab59513ad.tar.gz cpython-5c9a81a3d828e775a1500bb8d84dc1dab59513ad.tar.bz2 |
Fix a bug when there was a newline in the string expandtabs was called on.
This also catches another condition that can overflow.
Will backport.
-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 3570ef1..d38e4a9 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 d7ad5cc..3870343 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3322,7 +3322,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; @@ -3332,7 +3333,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 3481d19..ad33b8e 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -5705,7 +5705,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; @@ -5715,7 +5716,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; + } } } |