1
0
mirror of https://github.com/php/php-src.git synced 2026-04-29 19:23:22 +02:00

Bugfix #27276: When using str_replace to expand a string, count occurances of needle in haystack to avoid massive overallocation

This commit is contained in:
Sara Golemon
2004-02-23 20:06:01 +00:00
parent 11366f4886
commit 5144a1f522
+8 -1
View File
@@ -3001,7 +3001,14 @@ PHPAPI char *php_str_to_str_ex(char *haystack, int length,
if (str_len < needle_len) {
new_str = emalloc(length + 1);
} else {
new_str = safe_emalloc((length / needle_len + 1), str_len, 0);
int count = 0;
char *o = haystack, *endp = haystack + length;
while ((o = php_memnstr(o, needle, needle_len, endp))) {
o += needle_len;
count++;
}
new_str = safe_emalloc(count, str_len - needle_len, length + 1);
}
e = s = new_str;