Skip to content

ext/filter: apply options.default only when the filter fails - #164

Closed
iliaal wants to merge 1 commit into
masterfrom
filter-bool-default-and-parse-str-string
Closed

ext/filter: apply options.default only when the filter fails#164
iliaal wants to merge 1 commit into
masterfrom
filter-bool-default-and-parse-str-string

Conversation

@iliaal

@iliaal iliaal commented Jul 16, 2026

Copy link
Copy Markdown
Owner

php_zval_filter() decides whether to apply options.default by inspecting the filtered value for IS_FALSE, so a successful FILTER_VALIDATE_BOOL result of false gets replaced by the default. FILTER_THROW_ON_FAILURE already keys off the handler's return code, so the two features disagree about what counts as a failure:

input '0'     | FILTER_THROW_ON_FAILURE: does not throw | options.default: applied
input 'maybe' | FILTER_THROW_ON_FAILURE: throws         | options.default: applied

This makes RETURN_VALIDATION_FAILED report FAILURE for every validation failure and keys options.default off that, so both features share one notion of failure. Net -5 lines. After the change '0' is a success for both and 'maybe' is a failure for both.

BC break, so it targets master and carries an UPGRADING entry. Callers who want a valid false and a failure to stay distinct without a default still use FILTER_NULL_ON_FAILURE.

Open question before this goes upstream: it repurposes the handler return value added in 0b326dc. The comment in filter_private.h said FAILURE meant validation failed and an exception should be thrown; this widens it to validation failed, leaving the throw decision in php_zval_filter(). That is DanielEScherzer's design call as the FILTER_THROW_ON_FAILURE RFC author.

@iliaal
iliaal changed the base branch from master to PHP-8.4 July 16, 2026 11:26
@iliaal
iliaal force-pushed the filter-bool-default-and-parse-str-string branch from 1ceaf87 to fb3cad9 Compare July 16, 2026 11:26
php_zval_filter() decided whether to apply options.default by inspecting the
filtered value for IS_FALSE, so a successful FILTER_VALIDATE_BOOL result of
false was replaced by the default. FILTER_THROW_ON_FAILURE already keys off
the handler's return code, so the two disagreed: filter_var("0",
FILTER_VALIDATE_BOOL, FILTER_THROW_ON_FAILURE) does not throw, while the same
input with a default returns the default.

Have RETURN_VALIDATION_FAILED report FAILURE for every validation failure and
key options.default off that, so both features share one notion of failure.
@iliaal
iliaal force-pushed the filter-bool-default-and-parse-str-string branch from fb3cad9 to c6bbe00 Compare July 16, 2026 12:21
@iliaal iliaal changed the title ext/filter: fix bool default and parse_str with filter.default ext/filter: apply options.default only when the filter fails Jul 16, 2026
@iliaal
iliaal changed the base branch from PHP-8.4 to master July 16, 2026 12:22
@iliaal iliaal closed this Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant