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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user