Commit 8096dc69b52 for php
commit 8096dc69b5237b8d32fcb4bdc2fc80a2f075902a
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date: Fri Nov 27 07:24:49 2026 -0500
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
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 8b0f8ec572b..e7ce42d3882 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -3352,6 +3352,7 @@ static zend_string *php_str_to_str_i_ex(zend_string *haystack, const char *lc_ha
if (str_len > ZSTR_LEN(lc_needle)) {
new_str = zend_string_safe_falloc(count, str_len - ZSTR_LEN(lc_needle), ZSTR_LEN(haystack), 0, /* fallible */ true);
if (UNEXPECTED(!new_str)) {
+ zend_string_release_ex(lc_needle, 0);
return NULL;
}
} else {
diff --git a/ext/standard/tests/strings/str_ireplace_oom.phpt b/ext/standard/tests/strings/str_ireplace_oom.phpt
new file mode 100644
index 00000000000..3991f96cc7a
--- /dev/null
+++ b/ext/standard/tests/strings/str_ireplace_oom.phpt
@@ -0,0 +1,23 @@
+--TEST--
+str_ireplace() releases lowercased needle on OOM
+--SKIPIF--
+<?php
+if (getenv('USE_ZEND_ALLOC') === '0') die('skip requires ZendMM');
+?>
+--INI--
+memory_limit=4M
+--FILE--
+<?php
+$base = memory_get_usage();
+register_shutdown_function(function () use ($base) {
+ var_dump(memory_get_usage() - $base < 100000);
+});
+function trigger(): void {
+ $needle = str_repeat('A', 500000);
+ str_ireplace($needle, str_repeat('b', 1000000), str_repeat('a', 1000000));
+}
+trigger();
+?>
+--EXPECTF--
+Fatal error: Allowed memory size of 4194304 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d
+bool(true)