Skip to content

Commit

Permalink
- Change validating filters to return "null" on failure so that they …
Browse files Browse the repository at this point in the history
…can be

  distinguised from the value "false" which might be valid as well.
  • Loading branch information
Derick Rethans committed Oct 11, 2006
1 parent 4a8aaa1 commit db25a23
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 87 deletions.
44 changes: 22 additions & 22 deletions ext/filter/logical_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */

if (len == 0) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

Expand Down Expand Up @@ -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))) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
} else {
zval_dtor(value);
Z_TYPE_P(value) = IS_LONG;
Expand All @@ -237,7 +237,7 @@ void php_filter_boolean(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
PHP_FILTER_TRIM_DEFAULT(str, len, end);
} else {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

Expand Down Expand Up @@ -286,7 +286,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */

if (len < 1) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

Expand Down Expand Up @@ -425,7 +425,7 @@ void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */

stateError:
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}
/* }}} */

Expand All @@ -451,21 +451,21 @@ void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
if (!regexp_set) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'regexp' option missing");
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC);
if (!re) {
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);

/* 0 means that the vector is too small to hold all the captured substring offsets */
if (matches < 0) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}
}
/* }}} */
Expand All @@ -479,25 +479,25 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */

if (url == NULL) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

if ((flags & FILTER_FLAG_SCHEME_REQUIRED) && url->scheme == NULL) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}
if ((flags & FILTER_FLAG_HOST_REQUIRED) && url->host == NULL) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}
if ((flags & FILTER_FLAG_PATH_REQUIRED) && url->path == NULL) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}
if ((flags & FILTER_FLAG_QUERY_REQUIRED) && url->query == NULL) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}
php_url_free(url);
}
Expand All @@ -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);
if (!re) {
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);

/* 0 means that the vector is too small to hold all the captured substring offsets */
if (matches < 0) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
}

}
Expand Down Expand Up @@ -767,27 +767,27 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
mode = FORMAT_IPV4;
} else {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

if (flags & (FILTER_FLAG_IPV4 || FILTER_FLAG_IPV6)) {
/* Both formats are cool */
} else if ((flags & FILTER_FLAG_IPV4) && mode == FORMAT_IPV6) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
} else if ((flags & FILTER_FLAG_IPV6) && mode == FORMAT_IPV4) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

switch (mode) {
case FORMAT_IPV4:
if (!_php_filter_validate_ipv4(str, ip TSRMLS_CC)) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}

Expand All @@ -799,7 +799,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
(ip[0] == 192 && ip[1] == 168)
) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}
}
Expand All @@ -812,7 +812,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
(ip[0] >= 224 && ip[0] <= 255)
) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}
}
Expand All @@ -824,7 +824,7 @@ void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
res = _php_filter_validate_ipv6_(str TSRMLS_CC);
if (res < 1) {
zval_dtor(value);
ZVAL_BOOL(value, 0);
ZVAL_NULL(value);
return;
}
}
Expand Down
12 changes: 6 additions & 6 deletions ext/filter/tests/010.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ array(7) {
[1]=>
int(1)
[2]=>
bool(false)
NULL
[3]=>
int(-23234)
[4]=>
bool(false)
NULL
[5]=>
bool(false)
NULL
[6]=>
array(0) {
}
Expand All @@ -38,13 +38,13 @@ array(7) {
[1]=>
float(1.7)
[2]=>
bool(false)
NULL
[3]=>
float(-23234.123)
[4]=>
bool(false)
NULL
[5]=>
bool(false)
NULL
[6]=>
array(0) {
}
Expand Down
32 changes: 16 additions & 16 deletions ext/filter/tests/013.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ int(255)
int(7)
int(16711680)
int(438)
bool(false)
NULL
int(0)
int(0)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
NULL
NULL
NULL
NULL
NULL
NULL
NULL
int(6)
bool(false)
bool(false)
NULL
NULL
int(-1)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
NULL
NULL
NULL
NULL
NULL
NULL
Done
6 changes: 3 additions & 3 deletions ext/filter/tests/014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ echo "Done\n";
?>
--EXPECTF--
bool(false)
bool(false)
NULL
bool(false)
array(5) {
[0]=>
Expand All @@ -48,7 +48,7 @@ array(5) {
[4]=>
array(2) {
[0]=>
bool(false)
NULL
[1]=>
bool(false)
}
Expand All @@ -61,7 +61,7 @@ bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
NULL
bool(false)
bool(false)
bool(false)
Expand Down
12 changes: 6 additions & 6 deletions ext/filter/tests/015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ string(18) "file:///tmp/test.c"
string(26) "ftp://ftp.example.com/tmp/"
string(11) "/tmp/test.c"
string(1) "/"
bool(false)
NULL
string(6) "http:/"
string(5) "http:"
string(4) "http"
string(0) ""
string(2) "-1"
bool(false)
bool(false)
NULL
string(10) "http://qwe"
bool(false)
bool(false)
NULL
NULL
string(22) "http://www.example.com"
bool(false)
NULL
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"
Done
12 changes: 6 additions & 6 deletions ext/filter/tests/016.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ echo "Done\n";
--EXPECT--
string(5) "[email protected]"
string(17) "[email protected]"
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
NULL
NULL
NULL
NULL
NULL
NULL
string(57) "[email protected]"
Done
8 changes: 4 additions & 4 deletions ext/filter/tests/017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ echo "Done\n";
?>
--EXPECTF--
string(4) "data"
bool(false)
NULL
string(4) "data"
bool(false)
bool(false)
NULL
NULL

Warning: filter_var(): 'regexp' option missing in %s on line %d
bool(false)
NULL
Done
20 changes: 10 additions & 10 deletions ext/filter/tests/018.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ echo "Done\n";
?>
--EXPECT--
string(11) "192.168.0.1"
bool(false)
NULL
string(3) "::1"
string(7) "fe00::0"
bool(false)
bool(false)
NULL
NULL
string(9) "127.0.0.1"
bool(false)
NULL
string(12) "192.0.34.166"
string(9) "127.0.0.1"
string(9) "192.0.0.1"
string(12) "192.0.34.166"
bool(false)
NULL
string(15) "255.255.255.255"
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
NULL
NULL
NULL
NULL
NULL
string(3) "::1"
string(9) "127.0.0.1"
Done
8 changes: 4 additions & 4 deletions ext/filter/tests/019.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var_dump(filter_var("1.1.1.1", FILTER_VALIDATE_IP));
echo "Done\n";
?>
--EXPECTF--
bool(false)
bool(false)
bool(false)
bool(false)
NULL
NULL
NULL
NULL
string(7) "1.1.1.1"
Done
Loading

0 comments on commit db25a23

Please sign in to comment.