]> granicus.if.org Git - php/commitdiff
Use TypeError for preg_replace type check
authorNikita Popov <nikita.ppv@gmail.com>
Sun, 11 Aug 2019 12:28:10 +0000 (14:28 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Sun, 11 Aug 2019 12:28:10 +0000 (14:28 +0200)
This is a type violation warning, and as such should use TypeError
in PHP 8.

ext/pcre/php_pcre.c
ext/pcre/php_pcre.stub.php
ext/pcre/tests/bug21732.phpt
ext/pcre/tests/preg_replace2.phpt
ext/pcre/tests/preg_replace_error2.phpt

index 6002eedc782e887bd5230b86ed3c3704dc712d0d..4a603ec99d6a1ec01ce7db4de65d909f98ab04df 100644 (file)
@@ -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;
                }
        }
 
index f4730f1b6074be9eefba25eedfa6dfabd8937e7e..8a97cb451617bdd512390526f56b1338746b1af4 100644 (file)
@@ -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) {}
 
index 3dfc41e19fe9445f5b6ad14bee8c6f54d21a2ae2..629e015a06132b483d444f64c02d4447c3478c7a 100644 (file)
@@ -1,7 +1,5 @@
 --TEST--
 Bug #21732 (preg_replace() segfaults with invalid parameters)
---INI--
-error_reporting=0
 --FILE--
 <?php
 class foo {
@@ -11,11 +9,15 @@ class foo {
        }
 }
 
-var_dump(preg_replace('', array(), ''));
+try {
+    var_dump(preg_replace('', array(), ''));
+} catch (TypeError $e) {
+    echo $e->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"
index 4a191f3331a8fc0261e3db691be7ec09a7255043..13696827fb5019083457e57252ff09ccda38ddd7 100644 (file)
@@ -9,8 +9,6 @@ if (@preg_match('/./u', '') === false) {
 --FILE--
 <?php
 
-var_dump(preg_replace('', array(), ''));
-
 var_dump(preg_replace(array('/\da(.)/ui', '@..@'), '$1', '12Abc'));
 var_dump(preg_replace(array('/\da(.)/ui', '@(.)@'), '$1', array('x','a2aA', '1av2Ab')));
 
@@ -21,9 +19,7 @@ var_dump(preg_replace(array('/\s+/', '~[b-d]~'), array('$'), array('x y', 'bd bc
 echo "==done==\n";
 
 ?>
---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]=>
index a334b2fefdf6d743c3e51312d04588b1ff2bf1b0..0a3ba3bb46b92142afb0b75398ffcef61e685a11 100644 (file)
@@ -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