]> granicus.if.org Git - python/commitdiff
Backport 55874:
authorNeal Norwitz <nnorwitz@gmail.com>
Mon, 11 Jun 2007 04:32:41 +0000 (04:32 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Mon, 11 Jun 2007 04:32:41 +0000 (04:32 +0000)
Fix a bug when there was a newline in the string expandtabs was called on.
This also catches another condition that can overflow.

Lib/test/string_tests.py
Objects/stringobject.c
Objects/unicodeobject.c

index b257d57b0e08c06d1f91cbb28b76891381e61603..69a2225cb214551a86205e65070d52aaef66766b 100644 (file)
@@ -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'],
index 8b546431080fb63b04a7dcede3004c90f597edba..cee78a0089f4957bd29610291bb082783127a3be 100644 (file)
@@ -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;
+                }
             }
         }
 
index 0640da8cd6050862187d363a207cb5fe007eea3d..742db6ff52297a38f819a673749ff9cd1addb5b7 100644 (file)
@@ -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;
+                }
             }
         }