Bug #79726 [Opn]: SplStack::pop breaks stack validity

From: Date: Wed, 24 Jun 2020 08:08:00 +0000
Subject: Bug #79726 [Opn]: SplStack::pop breaks stack validity
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227617@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79726&edit=1 ID: 79726 Updated by: nikic@php.net 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: I guess we could check whether traverse_pointer is equal to the popped element and reset the pointer in that case. I'm inclined to agree that this is "Won't Fix" though. The provided code doesn't make sense. Previous Comments: ------------------------------------------------------------------------ [2020-06-24 07:55:15] cmb@php.net The behavior likely changed due to fixing bug #79151. In my opinion, this change is not a bug, but rather the given test code is invalid; valid alternatives would be <https://3v4l.org/fZKT8> and <https://3v4l.org/HLrZn>. ------------------------------------------------------------------------ [2020-06-24 07:03:33] millet dot gael at gmail dot com I fixed my code, but I think it's still a bug. Here is the weirdest part : https://3v4l.org/GPvYk The previous code is working with 7.2.0 - 7.3.14, 7.4.0 - 7.4.2 and fails with 7.3.15 - 7.3.19, 7.4.3 - 7.4.7 The behaviour is unpredictable depending of the php version. ------------------------------------------------------------------------ [2020-06-23 14:45:17] Wes dot example at example dot org 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#227617) next »