php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch consistent_filter_input_array for Filter related Bug #42608

Patch version 2012-02-27 14:38 UTC

Return to Bug #42608 | Download this patch
Patch Revisions:

Developer: datibbaw@php.net

Index: ext/filter/tests/055.phpt
===================================================================
--- ext/filter/tests/055.phpt	(revision 0)
+++ ext/filter/tests/055.phpt	(revision 0)
@@ -0,0 +1,20 @@
+--TEST--
+filter_input_array() - consistent result when GET or POST are not available
+--SKIPIF--
+<?php if (!extension_loaded("filter")) die("skip"); ?>
+--FILE--
+<?php
+
+var_dump(filter_input_array(INPUT_GET, array('foo' => FILTER_DEFAULT)));
+var_dump(filter_input_array(INPUT_POST, array('foo' => FILTER_DEFAULT)));
+
+?>
+--EXPECT--
+array(1) {
+  ["foo"]=>
+  NULL
+}
+array(1) {
+  ["foo"]=>
+  NULL
+}
\ No newline at end of file
Index: ext/filter/filter.c
===================================================================
--- ext/filter/filter.c	(revision 323585)
+++ ext/filter/filter.c	(working copy)
@@ -685,13 +685,21 @@
 	zval **tmp, **arg_elm;
 
 	if (!op) {
-		zval_dtor(return_value);
-		MAKE_COPY_ZVAL(&input, return_value);
-		php_filter_call(&return_value, FILTER_DEFAULT, NULL, 0, FILTER_REQUIRE_ARRAY TSRMLS_CC);
+		if (input) {
+			zval_dtor(return_value);
+			MAKE_COPY_ZVAL(&input, return_value);
+			php_filter_call(&return_value, FILTER_DEFAULT, NULL, 0, FILTER_REQUIRE_ARRAY TSRMLS_CC);
+		} else {
+			array_init(return_value);
+		}
 	} else if (Z_TYPE_PP(op) == IS_LONG) {
-		zval_dtor(return_value);
-		MAKE_COPY_ZVAL(&input, return_value);
-		php_filter_call(&return_value, Z_LVAL_PP(op), NULL, 0, FILTER_REQUIRE_ARRAY TSRMLS_CC);
+		if (input) {
+			zval_dtor(return_value);
+			MAKE_COPY_ZVAL(&input, return_value);
+			php_filter_call(&return_value, Z_LVAL_PP(op), NULL, 0, FILTER_REQUIRE_ARRAY TSRMLS_CC);
+		} else {
+			array_init(return_value);
+		}
 	} else if (Z_TYPE_PP(op) == IS_ARRAY) {
 		array_init(return_value);
 
@@ -710,11 +718,7 @@
 				zval_dtor(return_value);
 				RETURN_FALSE;
 			}
-			if (zend_hash_find(Z_ARRVAL_P(input), arg_key, arg_key_len, (void **)&tmp) != SUCCESS) {
-				if (add_empty) {
-					add_assoc_null_ex(return_value, arg_key, arg_key_len);
-				}
-			} else {
+			if (input && zend_hash_find(Z_ARRVAL_P(input), arg_key, arg_key_len, (void **)&tmp) == SUCCESS) {
 				zval *nval;
 
 				ALLOC_ZVAL(nval);
@@ -722,6 +726,8 @@
 
 				php_filter_call(&nval, -1, arg_elm, 0, FILTER_REQUIRE_SCALAR TSRMLS_CC);
 				add_assoc_zval_ex(return_value, arg_key, arg_key_len, nval);
+			} else if (add_empty) {
+				add_assoc_null_ex(return_value, arg_key, arg_key_len);
 			}
 		}
 	} else {
@@ -832,29 +838,6 @@
 
 	array_input = php_filter_get_storage(fetch_from TSRMLS_CC);
 
-	if (!array_input || !HASH_OF(array_input)) {
-		long filter_flags = 0;
-		zval **option;
-		if (op) {
-			if (Z_TYPE_PP(op) == IS_LONG) {
-				filter_flags = Z_LVAL_PP(op);
-			} else if (Z_TYPE_PP(op) == IS_ARRAY && zend_hash_find(HASH_OF(*op), "flags", sizeof("flags"), (void **)&option) == SUCCESS) {
-				PHP_FILTER_GET_LONG_OPT(option, filter_flags);
-			}
-		}
-
-		/* The FILTER_NULL_ON_FAILURE flag inverts the usual return values of
-		 * the function: normally when validation fails false is returned, and
-		 * when the input value doesn't exist NULL is returned. With the flag
-		 * set, NULL and false should be returned, respectively. Ergo, although
-		 * the code below looks incorrect, it's actually right. */
-		if (filter_flags & FILTER_NULL_ON_FAILURE) {
-			RETURN_FALSE;
-		} else {
-			RETURN_NULL();
-		}
-	}
-
 	php_filter_array_handler(array_input, op, return_value, add_empty TSRMLS_CC);
 }
 /* }}} */
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Apr 12 23:01:26 2021 UTC