|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68825 Inconsistent exception in DirectoryIterator::getLinkTarget()
Submitted: 2015-01-13 12:01 UTC Modified: 2018-08-19 15:45 UTC
Avg. Score:4.0 ± 0.8
Reproduced:1 of 3 (33.3%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: daniel dot jaenecke at sedo dot com Assigned: cmb (profile)
Status: Closed Package: SPL related
PHP Version: 5.6.4 OS: Linux
Private report: No CVE-ID: None
 [2015-01-13 12:01 UTC] daniel dot jaenecke at sedo dot com
The test script provided below will fail with a RuntimeException:
PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'SplFileInfo::getLinkTarget(): Empty filename' in test.php:10

What makes things really nasty: adding any of these statements before $entry->getLinkTarget() will make things work, there will be no exception being thrown:
 - readlink($entry->getPathName())
 - $entry->isLink()

Test script:
symlink('/no/such/directory', 'foo');

$file = new \SplFileInfo('foo');

$di = new \DirectoryIterator('.');
foreach($di as $entry) {
    if('foo' === $entry->getFilename()) {

Expected result:
I expect a consistent behavior. 
Either a call to getLinkTarget() on an invalid symlink shall always result in an exception or - what I would prefer - never. But the behavior should neither change depending on whether the DirectoryIterator is used or SplFileInfo directly; nor should it change depending on other methods/function calls.

The documentation for SplFileInfo::getLinkTarget() is also not consistent with that behavior, it says:
"The target may not be the real path on the filesystem. Use SplFileInfo::getRealPath() to determine the true path on the filesystem."
But the described behavior looks like a call to realpath was already executed internally.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-28 12:20 UTC]
-Package: SPL_Types +Package: SPL related
 [2018-08-18 16:44 UTC]
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: cmb
 [2018-08-18 16:44 UTC]
> But the described behavior looks like a call to realpath was
> already executed internally.

No realpath is involved here.  It has just been overlooked to
initialize intern->file_name, so the methods behaves

[1] <>
 [2018-08-22 13:41 UTC]
Automatic comment on behalf of
Log: Fix #68825: Exception in DirectoryIterator::getLinkTarget()
 [2018-08-22 13:41 UTC]
-Status: Analyzed +Status: Closed
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue May 30 02:03:44 2023 UTC