Skip to content

Commit 503b9a9

Browse files
Throw ValueError when a wrong flag value is provided to the second argument of scandir()
1 parent d0274e7 commit 503b9a9

File tree

5 files changed

+21
-138
lines changed

5 files changed

+21
-138
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ PHP 8.5 UPGRADE NOTES
142142
. Using a printf-family function with a formatter that did not specify the
143143
precision previously incorrectly reset the precision instead of treating
144144
it as a precision of 0. See GH-18897.
145+
. Passing an invalid flag value to the second argument of scandir() will now
146+
throw a ValueError. Previously, it would silently use the
147+
SCANDIR_SORT_DESCENDING value.
145148

146149
========================================
147150
2. New Features

ext/standard/dir.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,13 @@ PHP_FUNCTION(scandir)
549549
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
550550
} else if (flags == PHP_SCANDIR_SORT_NONE) {
551551
n = php_stream_scandir(dirn, &namelist, context, NULL);
552-
} else {
552+
} else if (flags == PHP_SCANDIR_SORT_DESCENDING) {
553553
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
554-
}
554+
} else {
555+
zend_argument_value_error(2, "must be one of the SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING and SCANDIR_SORT_NONE constants");
556+
RETURN_THROWS();
557+
}
558+
555559
if (n < 0) {
556560
php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
557561
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 and 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)