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)