Bug #36365 [Com]: scandir duplicates file name at every 65535th file

From: Date: Thu, 23 Jul 2015 05:59:34 +0000
Subject: Bug #36365 [Com]: scandir duplicates file name at every 65535th file
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194646@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 Comment by: cbader92 at gmail dot com Reported by: pilhoon at gmail dot com Summary: scandir duplicates file name at every 65535th file Status: Assigned Type: Bug Package: Directory function related Operating System: windows PHP Version: * Assigned To: pajoye Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [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". ------------------------------------------------------------------------ [2010-01-07 18:26:21] pajoye@php.net Please try using this snapshot: http://snaps.php.net/php5.3-latest.tar.gz For Windows: http://windows.php.net/snapshots/ ------------------------------------------------------------------------ [2010-01-07 18:02:51] enzo at smshome dot net Same problem here using PHP Version 5.2.5 (x64) with Windows XP 64bit Professional Edition. Duplicated files every 65535 files read using scandir(). I think you should put an alert on manual page: http://php.net/manual/en/function.scandir.php ------------------------------------------------------------------------ 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

« previous php.bugs (#194646) next »