diff options
| author | Sara Golemon <pollita@php.net> | 2004-02-23 20:06:01 +0000 |
|---|---|---|
| committer | Sara Golemon <pollita@php.net> | 2004-02-23 20:06:01 +0000 |
| commit | 5144a1f522e8fdee42c535ef80d6de21d0cd57cf (patch) | |
| tree | 7a34768072c091440597b0614faded926d9c09c7 | |
| parent | 11366f4886da1f8cbfd5fdcd5269440b04b2e120 (diff) | |
| download | php-git-5144a1f522e8fdee42c535ef80d6de21d0cd57cf.tar.gz | |
Bugfix #27276: When using str_replace to expand a string, count occurances of needle in haystack to avoid massive overallocation
| -rw-r--r-- | ext/standard/string.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/ext/standard/string.c b/ext/standard/string.c index 661dd8fa72..05649e2742 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -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; |
