Skip to content

Commit 168fef3

Browse files
Throw ValueError when a wrong flag value is provided to the second argument of scandir() (#19373)
1 parent e0e2963 commit 168fef3

File tree

6 files changed

+22
-138
lines changed

6 files changed

+22
-138
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ PHP NEWS
152152
. Add enum SortDirection. (timwolla)
153153
. pathinfo() raises a ValueError with an invalid $flags argument.
154154
(David Carlier)
155+
. Passing an invalid flag value to the second argument of scandir() will now
156+
throw a ValueError. (alexandre-daubois)
155157

156158
- Streams:
157159
. Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ PHP 8.6 UPGRADE NOTES
139139
- Standard:
140140
. pathinfo() now raises a ValueError when an invalid $flag argument
141141
value is passed.
142+
. scandir() now raises a ValueError when an invalid $sorting_order
143+
argument value is passed.
142144

143145
- Zip:
144146
. ZipArchive::extractTo now raises a TypeError for the

ext/standard/dir.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,13 @@ PHP_FUNCTION(scandir)
588588
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
589589
} else if (flags == PHP_SCANDIR_SORT_NONE) {
590590
n = php_stream_scandir(dirn, &namelist, context, NULL);
591-
} else {
591+
} else if (flags == PHP_SCANDIR_SORT_DESCENDING) {
592592
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
593-
}
593+
} else {
594+
zend_argument_value_error(2, "must be one of the SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING, or SCANDIR_SORT_NONE constants");
595+
RETURN_THROWS();
596+
}
597+
594598
if (n < 0) {
595599
php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
596600
RETURN_FALSE;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Provide wrong flags to scandir()
3+
--FILE--
4+
<?php
5+
try {
6+
scandir('something', -1);
7+
} catch (ValueError $e) {
8+
echo $e->getMessage() . "\n";
9+
}
10+
?>
11+
--EXPECT--
12+
scandir(): Argument #2 ($sorting_order) must be one of the SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING, or SCANDIR_SORT_NONE constants

ext/standard/tests/dir/scandir_variation9-win32-mb.phpt

Lines changed: 0 additions & 71 deletions
This file was deleted.

ext/standard/tests/dir/scandir_variation9.phpt

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)