Bug #36365 [Ana->Csd]: scandir duplicates file name at every 65535th file
| From: | cmb@php.net | Date: | Mon, 27 Jul 2015 23:07:56 +0000 |
| Subject: | Bug #36365 [Ana->Csd]: scandir duplicates file name at every 65535th file | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-194768@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=36365&edit=1
ID: 36365
Updated by: cmb@php.net
Reported by: pilhoon at gmail dot com
Summary: scandir duplicates file name at every 65535th file
-Status: Analyzed
+Status: Closed
Type: Bug
Package: Directory function related
Operating System: Windows only
PHP Version: *
Assigned To: pajoye
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4e8f01cb6e75e09ad7ffa5201c42d1bea2a61264
Log: Fix #36365: scandir duplicates file name at every 65535th file
Previous Comments:
------------------------------------------------------------------------
[2015-07-23 12:59:28] cmb@php.net
I've made a respective PR.
------------------------------------------------------------------------
[2015-07-23 05:59:31] cbader92 at gmail dot com
Can reproduce bug with PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) in Windows 7 Home Premium x64,
build 7601.
I thought I was going crazy. My 100,000 files generated an array with 100,001 indexes. Took forever
to find out why my script was trying to copy more files than there were in the directory.
------------------------------------------------------------------------
[2015-06-07 23:51:18] cmb@php.net
The problem is in readdir_r()[1]. If dp->offset != 0
FindNextFile() is called, then dp->offset is increased. Finally,
the directory entry is composed and "returned". However,
dp->offset is a short int[2], so once for every 65535 entries
FindNextFile() is not called, but a directory entry is added
nonetheless. Furthermore, dp->dent.d_off is not correct for high
values.
[1] <https://github.com/php/php-src/blob/php-5.6.9/win32/readdir.c#L92-L118>
[2] <https://github.com/php/php-src/blob/php-5.6.9/win32/readdir.h#L35>
------------------------------------------------------------------------
[2012-09-08 03:24:40] gobie at centrum dot cz
Same problem reproduced
PHP Version 5.4.4 Windows 7 x64
PHP Version 5.3.13 Windows 7 x32
But all PHP methods listing files are affected scandir, glob, DirectoryIterator.
Reproduce code:
---------------
// Settings
$dir = './test/';
$totalFiles = 1e5;
// Create empty files
!is_dir($dir) && mkdir($dir);
for ($i = 0; $i < $totalFiles; ++$i) {
$filename = $dir . str_pad($i, 6, '0', STR_PAD_LEFT);
touch($filename);
}
// Glob
$files = glob($dir . '*');
echo 'glob: ' . count($files) . '/' . $totalFiles . PHP_EOL;
// Scandir
$files = scandir($dir);
echo 'scandir: ' . (count($files) - 2) . '/' . $totalFiles . PHP_EOL; // . and
..
// DirectoryIterator
$it = new DirectoryIterator($dir);
echo 'DirectoryIterator: ' . (iterator_count($it) - 2) . '/' . $totalFiles .
PHP_EOL; // . and ..
unset($it);
Expected result:
----------------
glob: 100000/100000
scandir: 100000/100000
DirectoryIterator: 100000/100000
Actual result:
--------------
glob: 100001/100000
scandir: 100001/100000
DirectoryIterator: 100001/100000
------------------------------------------------------------------------
[2010-01-15 01:00:01] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=36365
--
Edit this bug report at https://bugs.php.net/bug.php?id=36365&edit=1