From 2f9d10c42b39883b433ff6601d7efca11e5f4871 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Fri, 27 Nov 2026 07:24:49 -0500 Subject: [PATCH] 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. --- ext/standard/string.c | 1 + .../tests/strings/str_ireplace_oom.phpt | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 ext/standard/tests/strings/str_ireplace_oom.phpt diff --git a/ext/standard/string.c b/ext/standard/string.c index 8b0f8ec572bb..e7ce42d38827 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 000000000000..3991f96cc7a5 --- /dev/null +++ b/ext/standard/tests/strings/str_ireplace_oom.phpt @@ -0,0 +1,23 @@ +--TEST-- +str_ireplace() releases lowercased needle on OOM +--SKIPIF-- + +--INI-- +memory_limit=4M +--FILE-- + +--EXPECTF-- +Fatal error: Allowed memory size of 4194304 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d +bool(true)