Skip to content

Commit

Permalink
Fix #73054: default option ignored when object passed to int filter
Browse files Browse the repository at this point in the history
If an object that can't be converted to string is validated, we must not
bail out early, but rather check for a requested default value.
  • Loading branch information
cmb69 committed Sep 9, 2016
1 parent cb91a51 commit 23e721f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PHP NEWS
FILTER_FLAG_NO_PRIV_RANGE). (julien)
. Fixed bug #67167 (Wrong return value from FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE). (levim, cmb)
. Fixed bug #73054 (default option ignored when object passed to int filter).
(cmb)

- GD:
. Fixed bug #67325 (imagetruecolortopalette: white is duplicated in palette).
Expand Down
5 changes: 3 additions & 2 deletions ext/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ static void php_zval_filter(zval **value, long filter, long flags, zval *options

ce = Z_OBJCE_PP(value);
if (!ce->__tostring) {
zval_ptr_dtor(value);
zval_dtor(*value);
/* #67167: doesn't return null on failure for objects */
if (flags & FILTER_NULL_ON_FAILURE) {
ZVAL_NULL(*value);
} else {
ZVAL_FALSE(*value);
}
return;
goto handle_default;
}
}

Expand All @@ -396,6 +396,7 @@ static void php_zval_filter(zval **value, long filter, long flags, zval *options

filter_func.function(*value, flags, options, charset TSRMLS_CC);

handle_default:
if (
options && (Z_TYPE_P(options) == IS_ARRAY || Z_TYPE_P(options) == IS_OBJECT) &&
((flags & FILTER_NULL_ON_FAILURE && Z_TYPE_PP(value) == IS_NULL) ||
Expand Down
23 changes: 23 additions & 0 deletions ext/filter/tests/bug73054.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Bug #73054 (default option ignored when object passed to int filter)
--SKIPIF--
<?php
if (!extension_loaded('filter')) die('skip filter extension not available');
?>
--FILE--
<?php
var_dump(
filter_var(new stdClass, FILTER_VALIDATE_INT, [
'options' => ['default' => 2],
]),
filter_var(new stdClass, FILTER_VALIDATE_INT, [
'options' => ['default' => 2],
'flags' => FILTER_NULL_ON_FAILURE
])
);
?>
===DONE===
--EXPECT--
int(2)
int(2)
===DONE===

0 comments on commit 23e721f

Please sign in to comment.