Commit 97e3d920 authored by Neal Norwitz's avatar Neal Norwitz

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.
parent 11aa6b17
...@@ -247,8 +247,13 @@ class CommonTest(unittest.TestCase): ...@@ -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')
self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8) 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('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) 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): def test_split(self):
self.checkequal(['this', 'is', 'the', 'split', 'function'], self.checkequal(['this', 'is', 'the', 'split', 'function'],
......
...@@ -3313,7 +3313,8 @@ string_expandtabs(PyStringObject *self, PyObject *args) ...@@ -3313,7 +3313,8 @@ string_expandtabs(PyStringObject *self, PyObject *args)
if (tabsize > 0) { if (tabsize > 0) {
j += tabsize - (j % tabsize); j += tabsize - (j % tabsize);
if (old_j > j) { if (old_j > j) {
PyErr_SetString(PyExc_OverflowError, "new string is too long"); PyErr_SetString(PyExc_OverflowError,
"new string is too long");
return NULL; return NULL;
} }
old_j = j; old_j = j;
...@@ -3323,7 +3324,12 @@ string_expandtabs(PyStringObject *self, PyObject *args) ...@@ -3323,7 +3324,12 @@ string_expandtabs(PyStringObject *self, PyObject *args)
j++; j++;
if (*p == '\n' || *p == '\r') { if (*p == '\n' || *p == '\r') {
i += j; i += j;
j = 0; old_j = j = 0;
if (i < 0) {
PyErr_SetString(PyExc_OverflowError,
"new string is too long");
return NULL;
}
} }
} }
......
...@@ -5701,7 +5701,8 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args) ...@@ -5701,7 +5701,8 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
if (tabsize > 0) { if (tabsize > 0) {
j += tabsize - (j % tabsize); j += tabsize - (j % tabsize);
if (old_j > j) { if (old_j > j) {
PyErr_SetString(PyExc_OverflowError, "new string is too long"); PyErr_SetString(PyExc_OverflowError,
"new string is too long");
return NULL; return NULL;
} }
old_j = j; old_j = j;
...@@ -5711,7 +5712,12 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args) ...@@ -5711,7 +5712,12 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
j++; j++;
if (*p == '\n' || *p == '\r') { if (*p == '\n' || *p == '\r') {
i += j; i += j;
j = 0; old_j = j = 0;
if (i < 0) {
PyErr_SetString(PyExc_OverflowError,
"new string is too long");
return NULL;
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment