Skip to content

Commit a5a1596

Browse files
cmb69smalyshev
authored andcommitted
Fix #78863: DirectoryIterator class silently truncates after a null byte
Since the constructor of DirectoryIterator and friends is supposed to accepts paths (i.e. strings without NUL bytes), we must not accept arbitrary strings.
1 parent d2cfb63 commit a5a1596

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

ext/spl/spl_directory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,10 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long cto
708708

709709
if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
710710
flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
711-
parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &path, &len, &flags);
711+
parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &path, &len, &flags);
712712
} else {
713713
flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
714-
parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &path, &len);
714+
parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &len);
715715
}
716716
if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_SKIPDOTS)) {
717717
flags |= SPL_FILE_DIR_SKIPDOTS;

ext/spl/tests/bug78863.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Bug #78863 (DirectoryIterator class silently truncates after a null byte)
3+
--FILE--
4+
<?php
5+
$dir = __DIR__ . '/bug78863';
6+
mkdir($dir);
7+
touch("$dir/bad");
8+
mkdir("$dir/sub");
9+
touch("$dir/sub/good");
10+
11+
$it = new DirectoryIterator(__DIR__ . "/bug78863\0/sub");
12+
foreach ($it as $fileinfo) {
13+
if (!$fileinfo->isDot()) {
14+
var_dump($fileinfo->getFilename());
15+
}
16+
}
17+
?>
18+
--EXPECTF--
19+
Fatal error: Uncaught UnexpectedValueException: DirectoryIterator::__construct() expects parameter 1 to be a valid path, string given in %s:%d
20+
Stack trace:
21+
#0 %s(%d): DirectoryIterator->__construct('%s')
22+
#1 {main}
23+
thrown in %s on line %d
24+
--CLEAN--
25+
<?php
26+
$dir = __DIR__ . '/bug78863';
27+
unlink("$dir/sub/good");
28+
rmdir("$dir/sub");
29+
unlink("$dir/bad");
30+
rmdir($dir);
31+
?>

0 commit comments

Comments
 (0)