php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51344 FILTER_NULL_ON_FAILURE flag automatically set in filter_input() functions.
Submitted: 2010-03-21 18:02 UTC Modified: 2010-04-20 06:25 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: pravila at alumni dot calpoly dot edu Assigned: aharvey (profile)
Status: Wont fix Package: Filter related
PHP Version: 5.2.13 OS: Linux
Private report: No CVE-ID: None
 [2010-03-21 18:02 UTC] pravila at alumni dot calpoly dot edu
Description:
------------
* This is different than bug http://bugs.php.net/bug.php?id=41305 *

The filter_var() vs. the filter_input() behave differently when using the 
FILTER_VALIDATE_BOOLEAN filter when the variable/input doesn't exist.

More specifically, it seems as if the FILTER_NULL_ON_FAILURE flag is set 
automatically in the filter_input() function.

(Note: same behavior for filter_var_array() vs. filter_input_array()).

From PHPINFO():
filter.default = unsafe_raw
filter.default_flags = no value
Revision: 1.52.2.39.2.16

Test script:
---------------
<?php
// example.com/script.php?arg1=yes&arg3=no

// filtering by variable
$var1 = filter_var($_GET["arg1"], FILTER_VALIDATE_BOOLEAN);
$var2 = filter_var($_GET["arg2"], FILTER_VALIDATE_BOOLEAN);
$var3 = filter_var($_GET["arg3"], FILTER_VALIDATE_BOOLEAN);

// filtering by input
$input1 = filter_input(INPUT_GET, "arg1", FILTER_VALIDATE_BOOLEAN);
$input2 = filter_input(INPUT_GET, "arg2", FILTER_VALIDATE_BOOLEAN);
$input3 = filter_input(INPUT_GET, "arg3", FILTER_VALIDATE_BOOLEAN);

// as expected...
var_dump($var1);      // bool(true)
var_dump($var2);      // bool(false)
var_dump($var3);      // bool(false)

// NULL is not an expected return unless the FILTER_NULL_ON_FAILURE flag is set...
var_dump($input1);    // bool(true)
var_dump($input2);    // NULL
var_dump($input3);    // bool(false)
?>

Expected result:
----------------
As per the documentation, we expect the output of the code above to be:

bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)

Actual result:
--------------
Even though the FILTER_NULL_ON_FAILURE flag is NOT set, we DO get a NULL value in 
the output:

bool(true)
bool(false)
bool(false)
bool(true)
NULL
bool(false)

Patches

bug51344-fix-wrong-return-value-for-null-flag (last revision 2010-04-10 23:13 UTC by mats dot lindh at gmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-11 01:15 UTC] mats dot lindh at gmail dot com
Patch to solve issue has been added. Patch is against current trunk but can probably be applied cleanly against 5.2 and 5.3 too. Issue stems from a simple error where RETURN_NULL(); and RETURN_FALSE; statements seems to have gotten mixed up.

Test is also attached in patch, based on the example in the bug report.
 [2010-04-20 02:48 UTC] aharvey@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: aharvey
 [2010-04-20 06:25 UTC] aharvey@php.net
-Status: Assigned +Status: Wont fix
 [2010-04-20 06:25 UTC] aharvey@php.net
This is going to sound insane when you've looked at the underlying filter code, but this is actually correct according to the documentation: the default behaviour of filter_input() is to return NULL for non-existent inputs and false when validation fails, and FILTER_NULL_ON_FAILURE simply flips that behaviour to false for non-existent inputs and NULL on validation failure. (No, I don't have a clue where that would be useful either, and the name of the flag is unfortunate in the filter_input() context, since it implies that NULL wouldn't normally be returned. It makes more sense when used with filter_var(), which doesn't have the non-existent input case.)

A table showing the return value from filter_input() in the different cases follows:

                       | "yes" | "no"  | "invalid" | non-existent |
No flags               | TRUE  | FALSE | FALSE     | NULL         |
FILTER_NULL_ON_FAILURE | TRUE  | FALSE | NULL      | FALSE        |

I'll pop a comment into the filter_input() and filter_input_array() implementations to note that this is by design, even though the code does kind of look wrong.

Closing Won't Fix.
 [2010-04-20 06:31 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=298196
Log: Added explanatory comments to filter_input and filter_input_array to document
why some code that looks intuitively wrong is actually correct. Related to
bug #51344 (FILTER_NULL_ON_FAILURE flag automatically set in filter_input()
functions).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 00:01:32 2024 UTC