diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index b59bd87f92385..4efe0542b7638 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -1481,7 +1481,8 @@ ZEND_FRAMELESS_FUNCTION(preg_match, 2) /* Compile regex or get it from cache. */ pcre_cache_entry *pce; if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) { - RETURN_FALSE; + RETVAL_FALSE; + goto flf_clean; } pce->refcount++; diff --git a/ext/pcre/tests/preg_match_frameless_leak.phpt b/ext/pcre/tests/preg_match_frameless_leak.phpt new file mode 100644 index 0000000000000..52bbfeceee0d1 --- /dev/null +++ b/ext/pcre/tests/preg_match_frameless_leak.phpt @@ -0,0 +1,26 @@ +--TEST-- +Memory leak in preg_match() frameless function with invalid regex and object arguments +--FILE-- +val = $val; + } + public function __toString() { + return $this->val; + } +} + +$regex = new Str("invalid regex"); +$subject = new Str("some subject"); + +// Running in a loop to ensure leak detection if run with memory tools +for ($i = 0; $i < 100; $i++) { + @preg_match($regex, $subject); +} + +echo "Done"; +?> +--EXPECT-- +Done