php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78223 DirectoryIterator::current returns itself, also other classes
Submitted: 2019-06-28 02:01 UTC Modified: 2019-06-28 02:12 UTC
From: zerkms at zerkms dot ru Assigned:
Status: Open Package: SPL related
PHP Version: 7.3.6 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: zerkms at zerkms dot ru
New email:
PHP Version: OS:

 

 [2019-06-28 02:01 UTC] zerkms at zerkms dot ru
Description:
------------
It's reproducible on all php >= 7 versions (including 7.4-rc)

I probably am missing something, but I cannot find it documented anywhere, hence the behaviour surprises me.

Test script:
---------------
<?php

$files = iterator_to_array(new DirectoryIterator(__DIR__));
var_dump($files[0]->getRealPath());

foreach (new DirectoryIterator(__DIR__) as $f) {
    var_dump($f->getRealPath());
    break;
}

Expected result:
----------------
string(4) "/app"
string(4) "/app"

Actual result:
--------------
bool(false)
string(4) "/app"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-28 02:12 UTC] requinix@php.net
-Summary: DirectoryIterator entries invalidated after its destruction +Summary: DirectoryIterator::current returns itself, also other classes
 [2019-06-28 02:12 UTC] requinix@php.net
Dump out $files and you'll see all entries in it are the same object: the DirectoryIterator itself. When iterator_to_array finishes the DirectoryIterator will be at the end of the list and so invalid, which then means all the (duplicate) values in the array are also invalid.

It's a quirk of how DirectoryIterator and some others work. I would love to see this fixed, but that may not be feasible...
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Apr 19 19:01:24 2021 UTC