Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ext/standard/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,13 @@ PHP_FUNCTION(scandir)
Z_PARAM_RESOURCE_OR_NULL(zcontext)
ZEND_PARSE_PARAMETERS_END();

if (flags != PHP_SCANDIR_SORT_ASCENDING &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just check this at the condition further down to not duplicate the checks?

flags != PHP_SCANDIR_SORT_DESCENDING &&
flags != PHP_SCANDIR_SORT_NONE) {
zend_argument_value_error(2, "must be one of SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING, or SCANDIR_SORT_NONE");
RETURN_THROWS();
}

if (dirn_len < 1) {
zend_argument_must_not_be_empty_error(1);
RETURN_THROWS();
Expand Down
42 changes: 42 additions & 0 deletions ext/standard/tests/dir/scandir_invalid_flag.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
scandir() flag validation and behavior
--FILE--
<?php

echo "== Valid flags ==\n";

$valid_flags = [
SCANDIR_SORT_ASCENDING,
SCANDIR_SORT_DESCENDING,
SCANDIR_SORT_NONE,
];

foreach ($valid_flags as $flag) {
$result = scandir(__DIR__, $flag);
echo is_array($result) ? "OK\n" : "FAIL\n";
}

echo "== Invalid flags ==\n";

$invalid_flags = [-1, 3, 999, PHP_INT_MAX];

foreach ($invalid_flags as $flag) {
try {
scandir(__DIR__, $flag);
echo "FAIL\n";
} catch (ValueError $e) {
echo "OK\n";
}
}

?>
--EXPECT--
== Valid flags ==
OK
OK
OK
== Invalid flags ==
OK
OK
OK
OK
Loading