php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #43529 readdir(), rewinddir() and closedir() don't always fail when passed 0 arguments
Submitted: 2007-12-07 15:38 UTC Modified: 2008-08-08 18:44 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: robin_fernandes at uk dot ibm dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.3CVS-2007-12-07 (snap) OS: Windows
Private report: No CVE-ID: None
 [2007-12-07 15:38 UTC] robin_fernandes at uk dot ibm dot com
Description:
------------
According to the docs, readdir(), rewinddir() and closedir() should take at least one argument: the directory handle.

However, these methods are also aliases for the Directory class methods Directory::read(), Directory::rewind() and Directory::close(). Instances of the Directory class hold the directory handle in $this->handle.

Therefore, if no argument is passed, the implementation of these methods  is such that $this->handle is used as the directory handle. However, no check is made to ensure $this is an instance of Directory.

Consequently, calling readdir() etc.. with no arguments from a context where $this->handle just happens to exists will result in unexpected behaviour instead of an error message.

Reproduce code:
---------------
<?php
Class C {
	public $handle;
	function test() {
		// Expecting failure.
		var_dump(readdir());
		var_dump(rewinddir());
		var_dump(closedir());
	}
}

$c = new C;
$c->handle = opendir(getcwd());
$c->test();
?>

Expected result:
----------------
Warning: readdir(): no Directory resource supplied in %s on line 6
bool(false)

Warning: rewinddir(): no Directory resource supplied in %s on line 7
bool(false)

Warning: closedir(): no Directory resource supplied in %s on line 8
bool(false)


Actual result:
--------------
string(1) "."
NULL
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-11 16:10 UTC] iliaa@php.net
The docs for these 3 functions (readdir(), rewinddir() and closedir()) 
are wrong. The code allows the parameter to be empty, in which case the 
currently opendir from the last opendir() call is used.
 [2008-08-08 18:44 UTC] kalle@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 20:01:33 2025 UTC