From:             maciej dot sz at gmail dot com
Operating system: irrelevant
PHP version:      5.4.7
Package:          SPL related
Bug Type:         Bug
Bug description:SplDoublyLinkedList should handle list modifications from 
outside of the object

Description:
------------
When I unset more then one element while iterating through
SplDoublyLinkedList object it throws an OutOfRangeException exception. Im
guessing that this is because each time an the offsetUnset() method is
called, the keys of the list are reset. This should not be happening until
the loop is rewind()'ed.

Test script:
---------------
$List = new SplDoublyLinkedList();

$List->push('a');
$List->push('b');
$List->push('c');
$List->push('d');

foreach ( $List as $key => $value ) {
    echo "Current element: key = {$key}, value = '{$value}'. ";
    if ( in_array($value, ['b', 'd']) ) {
        echo "MATCH! performing unset";
        unset($List[$key]);
    }
    echo "\n";
}

Expected result:
----------------
The elements should be removed from the list, and no exception should be
thrown. At the very least it should act as the ArrayObject object: triggers
a notice, but gets the job done. But the perfect solution would be the way
ArrayIterator does it - clean, no errors:

$Arr = new ArrayIterator(['a', 'b', 'c', 'd']);
foreach ( $Arr as $key => $value ) {
    echo "Current element: key = {$key}, value = '{$value}'. ";
    if ( in_array($value, ['b', 'd']) ) {
        echo "MATCH! performing unset";
        unset($Arr[$key]);
    }
    echo "\n";
}
var_dump($Arr->getArrayCopy());

Actual result:
--------------
OutOfRangeException

-- 
Edit bug report at https://bugs.php.net/bug.php?id=63154&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63154&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63154&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=63154&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=63154&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=63154&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=63154&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=63154&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=63154&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=63154&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=63154&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=63154&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=63154&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=63154&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63154&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=63154&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=63154&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=63154&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63154&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=63154&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63154&r=mysqlcfg

Reply via email to