Bug #79726 [Com]: SplStack::pop breaks stack validity
Edit report at https://bugs.php.net/bug.php?id=79726&edit=1
ID: 79726
Comment by: Wes dot example at example dot org
Reported by: millet dot gael at gmail dot com
Summary: SplStack::pop breaks stack validity
Status: Open
Type: Bug
Package: SPL related
Operating System: OSX
PHP Version: 7.4.7
Block user comment: N
Private report: N
New Comment:
The fact that worked before was probably accidental. Changing any data structure while iterating
over it, will produce this sort of errors. That's not just SPL or PHP, but happens in any
programming language, and it's disallowed by design in most of the times.
Your specific bug can be fixed, probably, but you should avoid that sort of code anyway. If you get
any kind of reasonable result from doing this, always consider it a hack, or accidental. Unless you
are actually iterating over a copy (like PHP arrays) or using something like ->filter()
array_filter() functions.
Previous Comments:
------------------------------------------------------------------------
[2020-06-23 14:25:49] millet dot gael at gmail dot com
Description:
------------
Breaking change on SplStack iterator.
If we pop from the stack, the stack become invalid for the last iteration.
This code was working with php 7.2.x, but is broken with 7.3.x and 7.4.x
Can be fixed by adding "$stack->rewind();" after "$stack->next();"
Test script:
---------------
<?php
$stack = new \SplStack();
$stack->push(1);
$stack->push(2);
$stack->rewind();
while ($stack->valid()) {
$value = $stack->pop();
var_dump($value);
$stack->next();
}
Expected result:
----------------
int(2)
int(1)
Actual result:
--------------
int(2)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79726&edit=1
Thread (6 messages)