Skip to content

Commit 9d9dffe

Browse files
committed
Fixed bug #79951
One branch did not release tmp_replace_entry_str. Also reduce the scope of some variables.
1 parent 07cb275 commit 9d9dffe

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ PHP NEWS
2727
. Fixed bug #79930 (array_merge_recursive() crashes when called with array
2828
with single reference). (Nikita)
2929
. Fixed bug #79944 (getmxrr always returns true on Alpine linux). (Nikita)
30+
. Fixed bug #79951 (Memory leak in str_replace of empty string). (Nikita)
3031

3132
- XML:
3233
. Fixed bug #79922 (Crash after multiple calls to xml_parser_free()). (cmb)

ext/standard/string.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,12 +4269,9 @@ PHPAPI void php_stripslashes(zend_string *str)
42694269
*/
42704270
static zend_long php_str_replace_in_subject(zval *search, zval *replace, zval *subject, zval *result, int case_sensitivity)
42714271
{
4272-
zval *search_entry,
4273-
*replace_entry = NULL;
4272+
zval *search_entry;
42744273
zend_string *tmp_result,
4275-
*tmp_subject_str,
4276-
*tmp_replace_entry_str = NULL,
4277-
*replace_entry_str;
4274+
*tmp_subject_str;
42784275
char *replace_value = NULL;
42794276
size_t replace_len = 0;
42804277
zend_long replace_count = 0;
@@ -4308,10 +4305,12 @@ static zend_long php_str_replace_in_subject(zval *search, zval *replace, zval *s
43084305
/* Make sure we're dealing with strings. */
43094306
zend_string *tmp_search_str;
43104307
zend_string *search_str = zval_get_tmp_string(search_entry, &tmp_search_str);
4308+
zend_string *replace_entry_str, *tmp_replace_entry_str = NULL;
43114309

43124310
/* If replace is an array. */
43134311
if (Z_TYPE_P(replace) == IS_ARRAY) {
43144312
/* Get current entry */
4313+
zval *replace_entry = NULL;
43154314
while (replace_idx < Z_ARRVAL_P(replace)->nNumUsed) {
43164315
replace_entry = &Z_ARRVAL_P(replace)->arData[replace_idx].val;
43174316
if (Z_TYPE_P(replace_entry) != IS_UNDEF) {
@@ -4368,15 +4367,12 @@ static zend_long php_str_replace_in_subject(zval *search, zval *replace, zval *s
43684367
}
43694368
} else {
43704369
zend_tmp_string_release(tmp_search_str);
4370+
zend_tmp_string_release(tmp_replace_entry_str);
43714371
continue;
43724372
}
43734373

43744374
zend_tmp_string_release(tmp_search_str);
4375-
4376-
if (tmp_replace_entry_str) {
4377-
zend_string_release_ex(tmp_replace_entry_str, 0);
4378-
tmp_replace_entry_str = NULL;
4379-
}
4375+
zend_tmp_string_release(tmp_replace_entry_str);
43804376

43814377
if (subject_str == tmp_result) {
43824378
zend_string_delref(subject_str);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Bug #79951: Memory leak in str_replace of empty string
3+
--FILE--
4+
<?php
5+
6+
var_dump(str_replace([""], [1000], "foo"));
7+
8+
?>
9+
--EXPECT--
10+
string(3) "foo"

0 commit comments

Comments
 (0)