]> granicus.if.org Git - php/commitdiff
Promote warnings to exceptions in ext/filter
authorMáté Kocsis <kocsismate@woohoolabs.com>
Mon, 10 Aug 2020 17:06:55 +0000 (19:06 +0200)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Mon, 24 Aug 2020 15:34:32 +0000 (17:34 +0200)
Closes GH-5970

ext/filter/callback_filter.c
ext/filter/filter.c
ext/filter/filter_private.h
ext/filter/logical_filters.c
ext/filter/tests/017.phpt
ext/filter/tests/029.phpt
ext/filter/tests/031.phpt
ext/filter/tests/039.phpt
ext/filter/tests/040.phpt
ext/filter/tests/055.phpt
ext/filter/tests/bug51368.phpt

index edba63e5627dd764b37c13c2fc09929d4e8c0c15..68b69e9680c098557cc03dff6d42ca89a24225f2 100644 (file)
@@ -23,7 +23,7 @@ void php_filter_callback(PHP_INPUT_FILTER_PARAM_DECL)
        int status;
 
        if (!option_array || !zend_is_callable(option_array, 0, NULL)) {
-               php_error_docref(NULL, E_WARNING, "First argument is expected to be a valid callback");
+               zend_type_error("%s(): Option must be a valid callback", get_active_function_name());
                zval_ptr_dtor(value);
                ZVAL_NULL(value);
                return;
index d049c34c4fbe2f61b69dd6bea868383192f5085f..37fac29de93d8fc7abb4e1ec38eb38c105428163 100644 (file)
@@ -487,8 +487,8 @@ static zval *php_filter_get_storage(zend_long arg)/* {{{ */
                        array_ptr = !Z_ISUNDEF(IF_G(env_array)) ? &IF_G(env_array) : &PG(http_globals)[TRACK_VARS_ENV];
                        break;
                default:
-                       php_error_docref(NULL, E_WARNING, "Unknown source");
-                       break;
+                       zend_argument_value_error(1, "must be an INPUT_* constant");
+                       return NULL;
        }
 
        if (array_ptr && Z_TYPE_P(array_ptr) != IS_ARRAY) {
@@ -512,6 +512,9 @@ PHP_FUNCTION(filter_has_var)
        }
 
        array_ptr = php_filter_get_storage(arg);
+       if (EG(exception)) {
+               RETURN_THROWS();
+       }
 
        if (array_ptr && zend_hash_exists(Z_ARRVAL_P(array_ptr), var)) {
                RETURN_TRUE;
@@ -614,14 +617,12 @@ static void php_filter_array_handler(zval *input, zval *op, zval *return_value,
 
                ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(op), arg_key, arg_elm) {
                        if (arg_key == NULL) {
-                               php_error_docref(NULL, E_WARNING, "Numeric keys are not allowed in the definition array");
-                               zval_ptr_dtor(return_value);
-                               RETURN_FALSE;
+                               zend_argument_type_error(2, "must contain only string keys");
+                               RETURN_THROWS();
                        }
                        if (ZSTR_LEN(arg_key) == 0) {
-                               php_error_docref(NULL, E_WARNING, "Empty keys are not allowed in the definition array");
-                               zval_ptr_dtor(return_value);
-                               RETURN_FALSE;
+                               zend_argument_value_error(2, "cannot contain empty keys");
+                               RETURN_THROWS();
                        }
                        if ((tmp = zend_hash_find(Z_ARRVAL_P(input), arg_key)) == NULL) {
                                if (add_empty) {
@@ -658,6 +659,9 @@ PHP_FUNCTION(filter_input)
        }
 
        input = php_filter_get_storage(fetch_from);
+       if (EG(exception)) {
+               RETURN_THROWS();
+       }
 
        if (!input || (tmp = zend_hash_find(Z_ARRVAL_P(input), var)) == NULL) {
                zend_long filter_flags = 0;
@@ -731,6 +735,9 @@ PHP_FUNCTION(filter_input_array)
        }
 
        array_input = php_filter_get_storage(fetch_from);
+       if (EG(exception)) {
+               RETURN_THROWS();
+       }
 
        if (!array_input) {
                zend_long filter_flags = 0;
index f8a02a490779708c21670af7bf4bea30f217046b..0db29dc698d924eeed2144e7072567b19eb32a72 100644 (file)
 || (id >= FILTER_VALIDATE_ALL && id <= FILTER_VALIDATE_LAST) \
 || id == FILTER_CALLBACK)
 
-#define RETURN_VALIDATION_FAILED       \
-       zval_ptr_dtor(value);   \
-       if (flags & FILTER_NULL_ON_FAILURE) {   \
-               ZVAL_NULL(value);       \
-       } else {        \
-               ZVAL_FALSE(value);      \
+#define RETURN_VALIDATION_FAILED \
+       if (EG(exception)) { \
+               return; \
+       } else if (flags & FILTER_NULL_ON_FAILURE) { \
+               zval_ptr_dtor(value); \
+               ZVAL_NULL(value); \
+       } else { \
+               zval_ptr_dtor(value); \
+               ZVAL_FALSE(value); \
        }       \
        return; \
 
index 76b0b977b37e7a7a01b1ee6e82ff78ee915498c3..a9fcc01d019726cd735b5430402a2a9663bed712 100644 (file)
@@ -360,7 +360,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
 
        if (decimal_set) {
                if (decimal_len != 1) {
-                       php_error_docref(NULL, E_WARNING, "Decimal separator must be one char");
+                       zend_value_error("%s(): \"decimal\" option must be one character long", get_active_function_name());
                        RETURN_VALIDATION_FAILED
                } else {
                        dec_sep = *decimal;
@@ -371,7 +371,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
 
        if (thousand_set) {
                if (thousand_len < 1) {
-                       php_error_docref(NULL, E_WARNING, "Thousand separator must be at least one char");
+                       zend_value_error("%s(): \"thousand\" option cannot be empty", get_active_function_name());
                        RETURN_VALIDATION_FAILED
                } else {
                        tsd_sep = thousand;
@@ -472,7 +472,7 @@ void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
        FETCH_STR_OPTION(regexp, "regexp");
 
        if (!regexp_set) {
-               php_error_docref(NULL, E_WARNING, "'regexp' option missing");
+               zend_value_error("%s(): \"regexp\" option is missing", get_active_function_name());
                RETURN_VALIDATION_FAILED
        }
 
@@ -919,7 +919,7 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
        FETCH_STRING_OPTION(exp_separator, "separator");
 
        if (exp_separator_set && exp_separator_len != 1) {
-               php_error_docref(NULL, E_WARNING, "Separator must be exactly one character long");
+               zend_value_error("%s(): \"separator\" option must be one character long", get_active_function_name());
                RETURN_VALIDATION_FAILED;
        }
 
index d11688ecebeb1c7f2a1fd2fcbd2d78d86401c8b1..c641e3cedcf39b94a1038277e04e221b4258e6b5 100644 (file)
@@ -10,17 +10,19 @@ var_dump(filter_var("data", FILTER_VALIDATE_REGEXP, array("options"=>array("rege
 var_dump(filter_var("data", FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>'/^d(.*)/'))));
 var_dump(filter_var("data", FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>'/blah/'))));
 var_dump(filter_var("data", FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>'/\[/'))));
-var_dump(filter_var("data", FILTER_VALIDATE_REGEXP));
+try {
+    filter_var("data", FILTER_VALIDATE_REGEXP);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 echo "Done\n";
 ?>
---EXPECTF--
+--EXPECT--
 string(4) "data"
 bool(false)
 string(4) "data"
 bool(false)
 bool(false)
-
-Warning: filter_var(): 'regexp' option missing in %s on line %d
-bool(false)
+filter_var(): "regexp" option is missing
 Done
index fc191f1710aa09d7601c008967bc9dc500ea3b27..53d7dd77f37fcfb73d2d49028781b2912303cecf 100644 (file)
@@ -13,9 +13,24 @@ function test($var) {
 var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test")));
 var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test")));
 var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test")));
-var_dump(filter_var("qwe", FILTER_CALLBACK, array("options"=>"no such func")));
-var_dump(filter_var("qwe", FILTER_CALLBACK, array("options"=>"")));
-var_dump(filter_var("qwe", FILTER_CALLBACK));
+
+try {
+    filter_var("qwe", FILTER_CALLBACK, array("options"=>"no such func"));
+} catch (TypeError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
+try {
+    filter_var("qwe", FILTER_CALLBACK, array("options"=>""));
+} catch (TypeError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
+try {
+    filter_var("qwe", FILTER_CALLBACK);
+} catch (TypeError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 /* Simple class method callback */
 class test_class {
@@ -62,15 +77,9 @@ echo "Done\n";
 string(4) "DATA"
 string(46) "~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?"}{:"
 string(0) ""
-
-Warning: filter_var(): First argument is expected to be a valid callback in %s on line %d
-NULL
-
-Warning: filter_var(): First argument is expected to be a valid callback in %s on line %d
-NULL
-
-Warning: filter_var(): First argument is expected to be a valid callback in %s on line %d
-NULL
+filter_var(): Option must be a valid callback
+filter_var(): Option must be a valid callback
+filter_var(): Option must be a valid callback
 string(4) "data"
 string(46) "~!@#$%^&*()_qwertyuiopasdfghjklzxcvbnm<>>?"}{:"
 string(0) ""
index 2968decafc763ed3ba79dab6f8dd6ba352961daf..0ef9f68930e6767dc89e7a0fe3abedaeec814faf 100644 (file)
@@ -33,12 +33,15 @@ $floats = array(
 
 echo "\ncustom decimal:\n";
 foreach ($floats as $float => $dec) {
-    $out = filter_var($float, FILTER_VALIDATE_FLOAT, array("options"=>array('decimal' => $dec)));
-    var_dump($out);
+    try {
+        var_dump(filter_var($float, FILTER_VALIDATE_FLOAT, array("options"=>array('decimal' => $dec))));
+    } catch (ValueError $exception) {
+        echo $exception->getMessage() . "\n";
+    }
 }
 
 ?>
---EXPECTF--
+--EXPECT--
 float(1.234)
 float(1.234)
 float(1.234)
@@ -52,7 +55,5 @@ custom decimal:
 bool(false)
 float(1.234)
 float(1.234)
-
-Warning: filter_var(): Decimal separator must be one char in %s on line %d
-bool(false)
+filter_var(): "decimal" option must be one character long
 bool(false)
index bdd090ae86f8e0f894c2e762589802254bf39c73..f5a1cf2fe7c2278b904deba2f54721389031d21e 100644 (file)
@@ -30,9 +30,16 @@ var_dump(filter_var_array(array(), array("var_name"=>-1)));
 var_dump(filter_var_array(array("var_name"=>""), array("var_name"=>-1)));
 
 echo "-- (5)\n";
-var_dump(filter_var_array(array("var_name"=>""), array("var_name"=>-1, "asdas"=>"asdasd", "qwe"=>"rty", ""=>"")));
-var_dump(filter_var_array(array("asdas"=>"text"), array("var_name"=>-1, "asdas"=>"asdasd", "qwe"=>"rty", ""=>"")));
-
+try {
+    filter_var_array(array("var_name"=>""), array("var_name"=>-1, "asdas"=>"asdasd", "qwe"=>"rty", ""=>""));
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+try {
+    filter_var_array(array("asdas"=>"text"), array("var_name"=>-1, "asdas"=>"asdasd", "qwe"=>"rty", ""=>""));
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
 
 $a = array(""=>""); $b = -1;
 var_dump(filter_var_array($a, $b));
@@ -48,7 +55,7 @@ var_dump($a, $b);
 
 echo "Done\n";
 ?>
---EXPECTF--
+--EXPECT--
 -- (1)
 array(0) {
 }
@@ -86,12 +93,8 @@ array(1) {
   string(0) ""
 }
 -- (5)
-
-Warning: filter_var_array(): Empty keys are not allowed in the definition array in %s on line %d
-bool(false)
-
-Warning: filter_var_array(): Empty keys are not allowed in the definition array in %s on line %d
-bool(false)
+filter_var_array(): Argument #2 ($options) cannot contain empty keys
+filter_var_array(): Argument #2 ($options) cannot contain empty keys
 bool(false)
 array(1) {
   [""]=>
index f6de3eb84cf6eac92df829eade42c9a52cbf1cd9..6f0a79199f2efc1d992cb58d6e2518116d6cf806 100644 (file)
@@ -16,12 +16,17 @@ var_dump(filter_has_var(INPUT_GET, "a"));
 var_dump(filter_has_var(INPUT_GET, "c"));
 var_dump(filter_has_var(INPUT_GET, "abc"));
 var_dump(filter_has_var(INPUT_GET, "cc"));
-var_dump(filter_has_var(-1, "cc"));
+try {
+    filter_has_var(-1, "cc");
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
 var_dump(filter_has_var(0, "cc"));
 
 echo "Done\n";
 ?>
---EXPECTF--
+--EXPECT--
 bool(false)
 bool(true)
 bool(true)
@@ -29,8 +34,6 @@ bool(true)
 bool(true)
 bool(false)
 bool(false)
-
-Warning: filter_has_var(): Unknown source in %s on line %d
-bool(false)
+filter_has_var(): Argument #1 ($type) must be an INPUT_* constant
 bool(false)
 Done
index 896dda833825bddbf030642f16a67b5330ae681c..0edc241efdc6a145dd4c7bdeddd27974c102a7d9 100644 (file)
@@ -21,12 +21,16 @@ $values = Array(
     array("01-23-45-67-89-ab", array("options" => array("separator" => ""))),
 );
 foreach ($values as $value) {
-    var_dump(filter_var($value[0], FILTER_VALIDATE_MAC, $value[1]));
+    try {
+        var_dump(filter_var($value[0], FILTER_VALIDATE_MAC, $value[1]));
+    } catch (ValueError $exception) {
+        echo $exception->getMessage() . "\n";
+    }
 }
 
 echo "Done\n";
 ?>
---EXPECTF--
+--EXPECT--
 string(17) "01-23-45-67-89-ab"
 string(17) "01-23-45-67-89-ab"
 bool(false)
@@ -39,10 +43,6 @@ string(17) "01:23:45:67:89:aB"
 bool(false)
 bool(false)
 string(14) "0123.4567.89ab"
-
-Warning: filter_var(): Separator must be exactly one character long in %s055.php on line %d
-bool(false)
-
-Warning: filter_var(): Separator must be exactly one character long in %s055.php on line %d
-bool(false)
+filter_var(): "separator" option must be one character long
+filter_var(): "separator" option must be one character long
 Done
index 38ae03ca95909c37aaa82ec91af0ade19799a0ea..ebe33a104183d0b15e95410c2380ce61e0dff4d8 100644 (file)
@@ -12,11 +12,15 @@ var_dump(
     filter_var('1 234.567', FILTER_VALIDATE_FLOAT, $options)
 );
 $options = ['flags' => FILTER_FLAG_ALLOW_THOUSAND, 'options' => ['thousand' => '']];
-var_dump(filter_var('12345', FILTER_VALIDATE_FLOAT, $options));
+
+try {
+    filter_var('12345', FILTER_VALIDATE_FLOAT, $options);
+} catch (ValueError $exception) {
+    echo $exception->getMessage() . "\n";
+}
+
 ?>
---EXPECTF--
+--EXPECT--
 float(1000)
 float(1234.567)
-
-Warning: filter_var(): Thousand separator must be at least one char in %s on line %d
-bool(false)
+filter_var(): "thousand" option cannot be empty