php.net |  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
Votes:3
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
Description:
------------
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:
---------------
<?php
symlink('/no/such/directory', 'foo');

$file = new \SplFileInfo('foo');
var_dump($file->getLinkTarget());

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


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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-28 12:20 UTC] cmb@php.net
-Package: SPL_Types +Package: SPL related
 [2018-08-18 16:44 UTC] cmb@php.net
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: cmb
 [2018-08-18 16:44 UTC] cmb@php.net
> 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
erroneously[1].

[1] <https://github.com/php/php-src/blob/php-7.3.0beta2/ext/spl/spl_directory.c#L1247-L1249>
 [2018-08-22 13:41 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=32a728d351aab4bac28790697cd6820a8843645f
Log: Fix #68825: Exception in DirectoryIterator::getLinkTarget()
 [2018-08-22 13:41 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Sep 26 10:01:25 2018 UTC