Skip to content

Commit 8096dc6

Browse files
committed
ext/standard: Release lc_needle on OOM in php_str_to_str_i_ex()
When zend_string_safe_falloc() fails under memory_limit, php_str_to_str_i_ex() returned NULL without releasing the lowercased needle. Closes GH-25848
1 parent 233f769 commit 8096dc6

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎ext/standard/string.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3352,6 +3352,7 @@ static zend_string *php_str_to_str_i_ex(zend_string *haystack, const char *lc_ha
33523352
if (str_len > ZSTR_LEN(lc_needle)) {
33533353
new_str = zend_string_safe_falloc(count, str_len - ZSTR_LEN(lc_needle), ZSTR_LEN(haystack), 0, /* fallible */ true);
33543354
if (UNEXPECTED(!new_str)) {
3355+
zend_string_release_ex(lc_needle, 0);
33553356
return NULL;
33563357
}
33573358
} else {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
str_ireplace() releases lowercased needle on OOM
3+
--SKIPIF--
4+
<?php
5+
if (getenv('USE_ZEND_ALLOC') === '0') die('skip requires ZendMM');
6+
?>
7+
--INI--
8+
memory_limit=4M
9+
--FILE--
10+
<?php
11+
$base = memory_get_usage();
12+
register_shutdown_function(function () use ($base) {
13+
var_dump(memory_get_usage() - $base < 100000);
14+
});
15+
function trigger(): void {
16+
$needle = str_repeat('A', 500000);
17+
str_ireplace($needle, str_repeat('b', 1000000), str_repeat('a', 1000000));
18+
}
19+
trigger();
20+
?>
21+
--EXPECTF--
22+
Fatal error: Allowed memory size of 4194304 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d
23+
bool(true)

0 commit comments

Comments
 (0)