Skip to content

Commit 1500f91

Browse files
author
amaury.forgeotdarc
committed
#3967: Correct a crash in count() and find() methods of string-like objects.
For example: "".count("xxxx", sys.maxint, 0) Reviewed by Benjamin Peterson. Will port to 2.5 and 3.0. git-svn-id: http://svn.python.org/projects/python/trunk@66631 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent f7cf8d0 commit 1500f91

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

Lib/test/string_tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def test_count(self):
120120
self.checkequal(2, 'aaa', 'count', '', -1)
121121
self.checkequal(4, 'aaa', 'count', '', -10)
122122

123+
self.checkequal(1, '', 'count', '')
124+
self.checkequal(0, '', 'count', '', 1, 1)
125+
self.checkequal(0, '', 'count', '', sys.maxint, 0)
126+
127+
self.checkequal(0, '', 'count', 'xx')
128+
self.checkequal(0, '', 'count', 'xx', 1, 1)
129+
self.checkequal(0, '', 'count', 'xx', sys.maxint, 0)
130+
123131
self.checkraises(TypeError, 'hello', 'count')
124132
self.checkraises(TypeError, 'hello', 'count', 42)
125133

@@ -169,6 +177,14 @@ def test_find(self):
169177
self.checkraises(TypeError, 'hello', 'find')
170178
self.checkraises(TypeError, 'hello', 'find', 42)
171179

180+
self.checkequal(0, '', 'find', '')
181+
self.checkequal(-1, '', 'find', '', 1, 1)
182+
self.checkequal(-1, '', 'find', '', sys.maxint, 0)
183+
184+
self.checkequal(-1, '', 'find', 'xx')
185+
self.checkequal(-1, '', 'find', 'xx', 1, 1)
186+
self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)
187+
172188
# For a variety of combinations,
173189
# verify that str.find() matches __contains__
174190
# and that the found substring is really at that location

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 final
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3967: Fixed a crash in the count() and find() methods of string-like
16+
objects, when the "start" parameter is a huge value.
17+
1518
- Issue #3965: Fixed a crash on Windows when open() is given an invalid
1619
filename or mode, and the filename is a unicode string.
1720

Objects/stringlib/count.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
1313
{
1414
Py_ssize_t count;
1515

16-
if (sub_len == 0) {
17-
if (str_len < 0)
18-
return 0; /* start > len(str) */
16+
if (str_len < 0)
17+
return 0; /* start > len(str) */
18+
if (sub_len == 0)
1919
return str_len + 1;
20-
}
2120

2221
count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT);
2322

Objects/stringlib/find.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ stringlib_find(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
1414
{
1515
Py_ssize_t pos;
1616

17-
if (sub_len == 0) {
18-
if (str_len < 0)
19-
return -1;
17+
if (str_len < 0)
18+
return -1;
19+
if (sub_len == 0)
2020
return offset;
21-
}
2221

2322
pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
2423

0 commit comments

Comments
 (0)