From: Nikita Popov Date: Sun, 11 Aug 2019 12:28:10 +0000 (+0200) Subject: Use TypeError for preg_replace type check X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=228bae7d74a7886f05716ef5ac0f3ba200caccc4;p=php Use TypeError for preg_replace type check This is a type violation warning, and as such should use TypeError in PHP 8. --- diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index 6002eedc78..4a603ec99d 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -2239,8 +2239,8 @@ static void preg_replace_common(INTERNAL_FUNCTION_PARAMETERS, int is_filter) } } else { if (Z_TYPE_P(regex) != IS_ARRAY) { - php_error_docref(NULL, E_WARNING, "Parameter mismatch, pattern is a string while replacement is an array"); - RETURN_FALSE; + zend_type_error("Parameter mismatch, pattern is a string while replacement is an array"); + return; } } diff --git a/ext/pcre/php_pcre.stub.php b/ext/pcre/php_pcre.stub.php index f4730f1b60..8a97cb4516 100644 --- a/ext/pcre/php_pcre.stub.php +++ b/ext/pcre/php_pcre.stub.php @@ -10,7 +10,7 @@ function preg_match_all(string $pattern, string $subject, &$subpatterns = null, * @param string|array $regex * @param string|array $replace * @param string|array $subject - * @return string|array|null|false + * @return string|array|null */ function preg_replace($regex, $replace, $subject, int $limit = -1, &$count = null) {} @@ -18,7 +18,7 @@ function preg_replace($regex, $replace, $subject, int $limit = -1, &$count = nul * @param string|array $regex * @param string|array $replace * @param string|array $subject - * @return string|array|null|false + * @return string|array|null */ function preg_filter($regex, $replace, $subject, int $limit = -1, &$count = null) {} diff --git a/ext/pcre/tests/bug21732.phpt b/ext/pcre/tests/bug21732.phpt index 3dfc41e19f..629e015a06 100644 --- a/ext/pcre/tests/bug21732.phpt +++ b/ext/pcre/tests/bug21732.phpt @@ -1,7 +1,5 @@ --TEST-- Bug #21732 (preg_replace() segfaults with invalid parameters) ---INI-- -error_reporting=0 --FILE-- getMessage(), "\n"; +} var_dump(preg_replace_callback("/(ab)(cd)(e)/", array(new foo(), "cb"), 'abcde')); ?> --EXPECT-- -bool(false) +Parameter mismatch, pattern is a string while replacement is an array array(4) { [0]=> string(5) "abcde" diff --git a/ext/pcre/tests/preg_replace2.phpt b/ext/pcre/tests/preg_replace2.phpt index 4a191f3331..13696827fb 100644 --- a/ext/pcre/tests/preg_replace2.phpt +++ b/ext/pcre/tests/preg_replace2.phpt @@ -9,8 +9,6 @@ if (@preg_match('/./u', '') === false) { --FILE-- ---EXPECTF-- -Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in %spreg_replace2.php on line 3 -bool(false) +--EXPECT-- string(1) "c" array(3) { [0]=> diff --git a/ext/pcre/tests/preg_replace_error2.phpt b/ext/pcre/tests/preg_replace_error2.phpt index a334b2fefd..0a3ba3bb46 100644 --- a/ext/pcre/tests/preg_replace_error2.phpt +++ b/ext/pcre/tests/preg_replace_error2.phpt @@ -16,7 +16,11 @@ $replace = array('this is a string', array('this is', 'a subarray'),); $subject = 'test'; foreach($replace as $value) { print "\nArg value is: $value\n"; - var_dump(preg_replace($regex, $value, $subject)); + try { + var_dump(preg_replace($regex, $value, $subject)); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } } $value = new stdclass(); //Object try { @@ -33,8 +37,6 @@ Arg value is: this is a string string(64) "this is a stringthis is a stringthis is a stringthis is a string" Arg value is: Array - -Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in %spreg_replace_error2.php on line %d -bool(false) +Parameter mismatch, pattern is a string while replacement is an array Object of class stdClass could not be converted to string Done