Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49ef6dc

Browse files
committed
Marc-Andre Lemburg <[email protected]>:
Fixed a bug in PyUnicode_Count() which would have caused a core dump in case of substring coercion failure. Synchronized .count() with the string method of the same name to return len(s)+1 for s.count('').
1 parent 2f4d0e9 commit 49ef6dc

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

Objects/unicodeobject.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,9 @@ int count(PyUnicodeObject *self,
21062106
{
21072107
int count = 0;
21082108

2109+
if (substring->length == 0)
2110+
return (end - start + 1);
2111+
21092112
end -= substring->length;
21102113

21112114
while (start <= end)
@@ -2130,7 +2133,7 @@ int PyUnicode_Count(PyObject *str,
21302133
return -1;
21312134
substr = PyUnicode_FromObject(substr);
21322135
if (substr == NULL) {
2133-
Py_DECREF(substr);
2136+
Py_DECREF(str);
21342137
return -1;
21352138
}
21362139

@@ -3086,11 +3089,6 @@ unicode_count(PyUnicodeObject *self, PyObject *args)
30863089
if (substring == NULL)
30873090
return NULL;
30883091

3089-
if (substring->length == 0) {
3090-
Py_DECREF(substring);
3091-
return PyInt_FromLong((long) 0);
3092-
}
3093-
30943092
if (start < 0)
30953093
start += self->length;
30963094
if (start < 0)

0 commit comments

Comments
 (0)