Skip to content

Commit f2e9368

Browse files
committed
Merged revisions 66631 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r66631 | amaury.forgeotdarc | 2008-09-27 00:34:08 +0200 (sam., 27 sept. 2008) | 7 lines python#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. ........
1 parent 60320cb commit f2e9368

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Lib/test/string_tests.py

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

110+
self.checkequal(1, '', 'count', '')
111+
self.checkequal(0, '', 'count', '', 1, 1)
112+
self.checkequal(0, '', 'count', '', sys.maxsize, 0)
113+
114+
self.checkequal(0, '', 'count', 'xx')
115+
self.checkequal(0, '', 'count', 'xx', 1, 1)
116+
self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0)
117+
110118
self.checkraises(TypeError, 'hello', 'count')
111119
self.checkraises(TypeError, 'hello', 'count', 42)
112120

@@ -156,6 +164,14 @@ def test_find(self):
156164
self.checkraises(TypeError, 'hello', 'find')
157165
self.checkraises(TypeError, 'hello', 'find', 42)
158166

167+
self.checkequal(0, '', 'find', '')
168+
self.checkequal(-1, '', 'find', '', 1, 1)
169+
self.checkequal(-1, '', 'find', '', sys.maxsize, 0)
170+
171+
self.checkequal(-1, '', 'find', 'xx')
172+
self.checkequal(-1, '', 'find', 'xx', 1, 1)
173+
self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0)
174+
159175
# For a variety of combinations,
160176
# verify that str.find() matches __contains__
161177
# and that the found substring is really at that location

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)