-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/standard: validate mode in array_filter() #15647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jorgsowa
wants to merge
2
commits into
php:master
Choose a base branch
from
jorgsowa:check-mode-in-array-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6500,7 +6500,7 @@ PHP_FUNCTION(array_filter) | |
zval args[2]; | ||
zval retval; | ||
bool have_callback = 0; | ||
zend_long use_type = 0; | ||
zend_long mode = ARRAY_FILTER_USE_VALUE; | ||
zend_string *string_key; | ||
zend_fcall_info fci = empty_fcall_info; | ||
zend_fcall_info_cache fci_cache = empty_fcall_info_cache; | ||
|
@@ -6510,19 +6510,30 @@ PHP_FUNCTION(array_filter) | |
Z_PARAM_ARRAY(array) | ||
Z_PARAM_OPTIONAL | ||
Z_PARAM_FUNC_OR_NULL(fci, fci_cache) | ||
Z_PARAM_LONG(use_type) | ||
Z_PARAM_LONG(mode) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
switch (mode) { | ||
case ARRAY_FILTER_USE_VALUE: | ||
case ARRAY_FILTER_USE_BOTH: | ||
case ARRAY_FILTER_USE_KEY: | ||
break; | ||
default: | ||
zend_argument_value_error(3, "must be a valid mode"); | ||
RETURN_THROWS(); | ||
} | ||
|
||
if (zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) { | ||
RETVAL_EMPTY_ARRAY(); | ||
return; | ||
} | ||
|
||
array_init(return_value); | ||
|
||
if (ZEND_FCI_INITIALIZED(fci)) { | ||
have_callback = 1; | ||
fci.retval = &retval; | ||
if (use_type == ARRAY_FILTER_USE_BOTH) { | ||
if (mode == ARRAY_FILTER_USE_BOTH) { | ||
fci.param_count = 2; | ||
key = &args[1]; | ||
} else { | ||
|
@@ -6533,15 +6544,15 @@ PHP_FUNCTION(array_filter) | |
|
||
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, operand) { | ||
if (have_callback) { | ||
if (use_type) { | ||
if (mode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to be explicit, you should use |
||
/* Set up the key */ | ||
if (!string_key) { | ||
ZVAL_LONG(key, num_key); | ||
} else { | ||
ZVAL_STR_COPY(key, string_key); | ||
} | ||
} | ||
if (use_type != ARRAY_FILTER_USE_KEY) { | ||
if (mode != ARRAY_FILTER_USE_KEY) { | ||
ZVAL_COPY(&args[0], operand); | ||
} | ||
fci.params = args; | ||
|
@@ -6550,7 +6561,7 @@ PHP_FUNCTION(array_filter) | |
bool retval_true; | ||
|
||
zval_ptr_dtor(&args[0]); | ||
if (use_type == ARRAY_FILTER_USE_BOTH) { | ||
if (mode == ARRAY_FILTER_USE_BOTH) { | ||
zval_ptr_dtor(&args[1]); | ||
} | ||
retval_true = zend_is_true(&retval); | ||
|
@@ -6560,7 +6571,7 @@ PHP_FUNCTION(array_filter) | |
} | ||
} else { | ||
zval_ptr_dtor(&args[0]); | ||
if (use_type == ARRAY_FILTER_USE_BOTH) { | ||
if (mode == ARRAY_FILTER_USE_BOTH) { | ||
zval_ptr_dtor(&args[1]); | ||
} | ||
return; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Test array_filter() function : usage variations - mode exception | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
var_dump(array_filter([], mode: 999)); | ||
} catch (Throwable $e) { | ||
echo $e::class . ': '.$e->getMessage(), "\n"; | ||
} | ||
|
||
echo "Done" | ||
?> | ||
--EXPECT-- | ||
ValueError: array_filter(): Argument #3 ($mode) must be a valid mode | ||
Done |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid the rename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't mode a more relevant name than
use_type
that suggests boolean value?This only touches internal variable, so I thought that shouldn't be a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
use_type
refers to "ARRAY_FILTER_USE_", somode
doesn't seem more descriptive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mode
is the literal name of the argument in thearray_filter
function.https://www.php.net/manual/en/function.array-filter.php
For me, it doesn't make a big difference, rather than alignment, so I can change it tomorrow.