php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75155 AppendIterator::append() is broken when appending another AppendIterator
Submitted: 2017-09-04 15:22 UTC Modified: 2017-09-05 08:53 UTC
From: bschussek at gmail dot com Assigned: nikic (profile)
Status: Closed Package: SPL related
PHP Version: 7.1.9 OS: ubuntu 16.04 LTS
Private report: No CVE-ID: None
 [2017-09-04 15:22 UTC] bschussek at gmail dot com
Description:
------------
When passing another AppendIterator to AppendIterator::append(), the entire AppendIterator is returned during iteration instead of the individual entries.

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

$array_a = new ArrayIterator(array('a', 'b', 'c'));
$array_b = new ArrayIterator(array('d', 'e', 'f'));

$iterator = new AppendIterator;
$iterator->append($array_a);

$iterator2 = new AppendIterator;
$iterator2->append($iterator);
$iterator2->append($array_b);

foreach ($iterator2 as $current) {
    echo $current;
}

Expected result:
----------------
abcdef

Actual result:
--------------
PHP Catchable fatal error:  Object of class ArrayIterator could not be converted to string

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-04 16:05 UTC] requinix@php.net
-Status: Open +Status: Verified
 [2017-09-04 16:05 UTC] requinix@php.net
New to 7.1.9 https://3v4l.org/egvFW and apparently created during the fix for bug #74977.
 [2017-09-04 19:28 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-09-05 08:22 UTC] codronm+circlecode at gmail dot com
please note that from 7.1.7 to 7.1.8,the behavior also changed: https://3v4l.org/EN75q
not sure this is related to the exact same bug (for which I was creating a report during the same time as this one appeared) or if this should be another one
 [2017-09-05 08:53 UTC] requinix@php.net
That's probably due to changes from #73471, however you're not quite following the contract: when iterating manually you need to call ->rewind() beforehand.

  foreach ($it as $key => $value) { ... }

translates to

  $it->rewind();
  while ($it->valid()) {
    $key = $it->key();
    $value = $it->current();
    ...
    $it->next();
  }

Adding the ->rewind() gives the correct behavior.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC