- Change validating filters to return "null" on failure so that they can be

distinguised from the value "false" which might be valid as well.
This commit is contained in:
Derick Rethans 2006-10-11 14:48:33 +00:00
parent 4a8aaa1e4d
commit db25a23ed8
13 changed files with 88 additions and 87 deletions

View file

@ -177,7 +177,7 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
if (len == 0) { if (len == 0) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
@ -217,7 +217,7 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
if (error > 0 || (min_range_set && (ctx_value < min_range)) || (max_range_set && (ctx_value > max_range))) { if (error > 0 || (min_range_set && (ctx_value < min_range)) || (max_range_set && (ctx_value > max_range))) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} else { } else {
zval_dtor(value); zval_dtor(value);
Z_TYPE_P(value) = IS_LONG; Z_TYPE_P(value) = IS_LONG;
@ -237,7 +237,7 @@ void php_filter_boolean(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
PHP_FILTER_TRIM_DEFAULT(str, len, end); PHP_FILTER_TRIM_DEFAULT(str, len, end);
} else { } else {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
@ -286,7 +286,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
if (len < 1) { if (len < 1) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
@ -425,7 +425,7 @@ stateT:
stateError: stateError:
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
/* }}} */ /* }}} */
@ -451,21 +451,21 @@ void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
if (!regexp_set) { if (!regexp_set) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'regexp' option missing"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "'regexp' option missing");
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC); re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC);
if (!re) { if (!re) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3); matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3);
/* 0 means that the vector is too small to hold all the captured substring offsets */ /* 0 means that the vector is too small to hold all the captured substring offsets */
if (matches < 0) { if (matches < 0) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
} }
/* }}} */ /* }}} */
@ -479,25 +479,25 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
if (url == NULL) { if (url == NULL) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
if ((flags & FILTER_FLAG_SCHEME_REQUIRED) && url->scheme == NULL) { if ((flags & FILTER_FLAG_SCHEME_REQUIRED) && url->scheme == NULL) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
if ((flags & FILTER_FLAG_HOST_REQUIRED) && url->host == NULL) { if ((flags & FILTER_FLAG_HOST_REQUIRED) && url->host == NULL) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
if ((flags & FILTER_FLAG_PATH_REQUIRED) && url->path == NULL) { if ((flags & FILTER_FLAG_PATH_REQUIRED) && url->path == NULL) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
if ((flags & FILTER_FLAG_QUERY_REQUIRED) && url->query == NULL) { if ((flags & FILTER_FLAG_QUERY_REQUIRED) && url->query == NULL) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
php_url_free(url); php_url_free(url);
} }
@ -518,14 +518,14 @@ void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
re = pcre_get_compiled_regex((char *)regexp, &pcre_extra, &preg_options TSRMLS_CC); re = pcre_get_compiled_regex((char *)regexp, &pcre_extra, &preg_options TSRMLS_CC);
if (!re) { if (!re) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3); matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3);
/* 0 means that the vector is too small to hold all the captured substring offsets */ /* 0 means that the vector is too small to hold all the captured substring offsets */
if (matches < 0) { if (matches < 0) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
} }
} }
@ -767,7 +767,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
mode = FORMAT_IPV4; mode = FORMAT_IPV4;
} else { } else {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
@ -775,11 +775,11 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
/* Both formats are cool */ /* Both formats are cool */
} else if ((flags & FILTER_FLAG_IPV4) && mode == FORMAT_IPV6) { } else if ((flags & FILTER_FLAG_IPV4) && mode == FORMAT_IPV6) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} else if ((flags & FILTER_FLAG_IPV6) && mode == FORMAT_IPV4) { } else if ((flags & FILTER_FLAG_IPV6) && mode == FORMAT_IPV4) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
@ -787,7 +787,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
case FORMAT_IPV4: case FORMAT_IPV4:
if (!_php_filter_validate_ipv4(str, ip TSRMLS_CC)) { if (!_php_filter_validate_ipv4(str, ip TSRMLS_CC)) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
@ -799,7 +799,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
(ip[0] == 192 && ip[1] == 168) (ip[0] == 192 && ip[1] == 168)
) { ) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
} }
@ -812,7 +812,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
(ip[0] >= 224 && ip[0] <= 255) (ip[0] >= 224 && ip[0] <= 255)
) { ) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
} }
@ -824,7 +824,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
res = _php_filter_validate_ipv6_(str TSRMLS_CC); res = _php_filter_validate_ipv6_(str TSRMLS_CC);
if (res < 1) { if (res < 1) {
zval_dtor(value); zval_dtor(value);
ZVAL_BOOL(value, 0); ZVAL_NULL(value);
return; return;
} }
} }

View file

@ -21,13 +21,13 @@ array(7) {
[1]=> [1]=>
int(1) int(1)
[2]=> [2]=>
bool(false) NULL
[3]=> [3]=>
int(-23234) int(-23234)
[4]=> [4]=>
bool(false) NULL
[5]=> [5]=>
bool(false) NULL
[6]=> [6]=>
array(0) { array(0) {
} }
@ -38,13 +38,13 @@ array(7) {
[1]=> [1]=>
float(1.7) float(1.7)
[2]=> [2]=>
bool(false) NULL
[3]=> [3]=>
float(-23234.123) float(-23234.123)
[4]=> [4]=>
bool(false) NULL
[5]=> [5]=>
bool(false) NULL
[6]=> [6]=>
array(0) { array(0) {
} }

View file

@ -49,24 +49,24 @@ int(255)
int(7) int(7)
int(16711680) int(16711680)
int(438) int(438)
bool(false) NULL
int(0) int(0)
int(0) int(0)
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
int(6) int(6)
bool(false) NULL
bool(false) NULL
int(-1) int(-1)
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
Done Done

View file

@ -34,7 +34,7 @@ echo "Done\n";
?> ?>
--EXPECTF-- --EXPECTF--
bool(false) bool(false)
bool(false) NULL
bool(false) bool(false)
array(5) { array(5) {
[0]=> [0]=>
@ -48,7 +48,7 @@ array(5) {
[4]=> [4]=>
array(2) { array(2) {
[0]=> [0]=>
bool(false) NULL
[1]=> [1]=>
bool(false) bool(false)
} }
@ -61,7 +61,7 @@ bool(true)
bool(false) bool(false)
bool(true) bool(true)
bool(false) bool(false)
bool(false) NULL
bool(false) bool(false)
bool(false) bool(false)
bool(false) bool(false)

View file

@ -52,20 +52,20 @@ string(18) "file:///tmp/test.c"
string(26) "ftp://ftp.example.com/tmp/" string(26) "ftp://ftp.example.com/tmp/"
string(11) "/tmp/test.c" string(11) "/tmp/test.c"
string(1) "/" string(1) "/"
bool(false) NULL
string(6) "http:/" string(6) "http:/"
string(5) "http:" string(5) "http:"
string(4) "http" string(4) "http"
string(0) "" string(0) ""
string(2) "-1" string(2) "-1"
bool(false) bool(false)
bool(false) NULL
string(10) "http://qwe" string(10) "http://qwe"
bool(false) NULL
bool(false) NULL
string(22) "http://www.example.com" string(22) "http://www.example.com"
bool(false) NULL
string(42) "http://www.example.com/path/at/the/server/" string(42) "http://www.example.com/path/at/the/server/"
bool(false) NULL
string(40) "http://www.example.com/index.php?a=b&c=d" string(40) "http://www.example.com/index.php?a=b&c=d"
Done Done

View file

@ -22,11 +22,11 @@ echo "Done\n";
--EXPECT-- --EXPECT--
string(5) "a@b.c" string(5) "a@b.c"
string(17) "abuse@example.com" string(17) "abuse@example.com"
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
string(57) "QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET" string(57) "QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET"
Done Done

View file

@ -14,11 +14,11 @@ echo "Done\n";
?> ?>
--EXPECTF-- --EXPECTF--
string(4) "data" string(4) "data"
bool(false) NULL
string(4) "data" string(4) "data"
bool(false) NULL
bool(false) NULL
Warning: filter_var(): 'regexp' option missing in %s on line %d Warning: filter_var(): 'regexp' option missing in %s on line %d
bool(false) NULL
Done Done

View file

@ -27,24 +27,24 @@ echo "Done\n";
?> ?>
--EXPECT-- --EXPECT--
string(11) "192.168.0.1" string(11) "192.168.0.1"
bool(false) NULL
string(3) "::1" string(3) "::1"
string(7) "fe00::0" string(7) "fe00::0"
bool(false) NULL
bool(false) NULL
string(9) "127.0.0.1" string(9) "127.0.0.1"
bool(false) NULL
string(12) "192.0.34.166" string(12) "192.0.34.166"
string(9) "127.0.0.1" string(9) "127.0.0.1"
string(9) "192.0.0.1" string(9) "192.0.0.1"
string(12) "192.0.34.166" string(12) "192.0.34.166"
bool(false) NULL
string(15) "255.255.255.255" string(15) "255.255.255.255"
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
string(3) "::1" string(3) "::1"
string(9) "127.0.0.1" string(9) "127.0.0.1"
Done Done

View file

@ -12,9 +12,9 @@ var_dump(filter_var("1.1.1.1", FILTER_VALIDATE_IP));
echo "Done\n"; echo "Done\n";
?> ?>
--EXPECTF-- --EXPECTF--
bool(false) NULL
bool(false) NULL
bool(false) NULL
bool(false) NULL
string(7) "1.1.1.1" string(7) "1.1.1.1"
Done Done

View file

@ -45,10 +45,10 @@ float(7000)
float(0.007) float(0.007)
custom decimal: custom decimal:
bool(false) NULL
float(1.234) float(1.234)
float(1.234) float(1.234)
Warning: filter_var(): decimal separator must be one char in %s on line %d Warning: filter_var(): decimal separator must be one char in %s on line %d
bool(false) NULL
bool(false) NULL

View file

@ -15,7 +15,8 @@ $booleans = array(
'False' => false, 'False' => false,
'faLsE' => false, 'faLsE' => false,
'oFf' => false, 'oFf' => false,
'' => false
'' => null
); );
foreach($booleans as $val=>$exp) { foreach($booleans as $val=>$exp) {

View file

@ -9,5 +9,5 @@ $var="3".chr(0)."foo";
var_dump(filter_var($var, FILTER_VALIDATE_FLOAT)); var_dump(filter_var($var, FILTER_VALIDATE_FLOAT));
?> ?>
--EXPECTF-- --EXPECTF--
bool(false) NULL
bool(false) NULL

View file

@ -46,7 +46,7 @@ int(123)
int(-123) int(-123)
int(0) int(0)
int(123) int(123)
bool(false) NULL
float(-0.123) float(-0.123)
float(0) float(0)
@ -54,7 +54,7 @@ float(1.23)
float(-1.23) float(-1.23)
float(0) float(0)
float(1.23) float(1.23)
bool(false) NULL
bool(true) bool(true)
bool(false) bool(false)
@ -67,5 +67,5 @@ bool(false)
bool(true) bool(true)
bool(false) bool(false)
bool(true) bool(true)
bool(false) NULL
bool(false) NULL