Skip to content

Commit 5083bc6

Browse files
author
neal.norwitz
committed
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. git-svn-id: http://svn.python.org/projects/python/trunk@55874 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 21b2283 commit 5083bc6

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

Lib/test/string_tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ def test_expandtabs(self):
247247
self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
248248
self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
249249
self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
250+
self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
250251

251252
self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
253+
# This test is only valid when sizeof(int) == sizeof(void*) == 4.
254+
if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
255+
self.checkraises(OverflowError,
256+
'\ta\n\tb', 'expandtabs', sys.maxint)
252257

253258
def test_split(self):
254259
self.checkequal(['this', 'is', 'the', 'split', 'function'],

Objects/stringobject.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3322,7 +3322,8 @@ string_expandtabs(PyStringObject *self, PyObject *args)
33223322
if (tabsize > 0) {
33233323
j += tabsize - (j % tabsize);
33243324
if (old_j > j) {
3325-
PyErr_SetString(PyExc_OverflowError, "new string is too long");
3325+
PyErr_SetString(PyExc_OverflowError,
3326+
"new string is too long");
33263327
return NULL;
33273328
}
33283329
old_j = j;
@@ -3332,7 +3333,12 @@ string_expandtabs(PyStringObject *self, PyObject *args)
33323333
j++;
33333334
if (*p == '\n' || *p == '\r') {
33343335
i += j;
3335-
j = 0;
3336+
old_j = j = 0;
3337+
if (i < 0) {
3338+
PyErr_SetString(PyExc_OverflowError,
3339+
"new string is too long");
3340+
return NULL;
3341+
}
33363342
}
33373343
}
33383344

Objects/unicodeobject.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5705,7 +5705,8 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
57055705
if (tabsize > 0) {
57065706
j += tabsize - (j % tabsize);
57075707
if (old_j > j) {
5708-
PyErr_SetString(PyExc_OverflowError, "new string is too long");
5708+
PyErr_SetString(PyExc_OverflowError,
5709+
"new string is too long");
57095710
return NULL;
57105711
}
57115712
old_j = j;
@@ -5715,7 +5716,12 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
57155716
j++;
57165717
if (*p == '\n' || *p == '\r') {
57175718
i += j;
5718-
j = 0;
5719+
old_j = j = 0;
5720+
if (i < 0) {
5721+
PyErr_SetString(PyExc_OverflowError,
5722+
"new string is too long");
5723+
return NULL;
5724+
}
57195725
}
57205726
}
57215727

0 commit comments

Comments
 (0)