Bug #80917 [Opn->Fbk]: LimitIterator does not end at count

From: Date: Mon, 29 Mar 2021 20:20:00 +0000
Subject: Bug #80917 [Opn->Fbk]: LimitIterator does not end at count
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233056@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80917&edit=1 ID: 80917 Updated by: levim@php.net Reported by: alexandreparent_dev at outlook dot com Summary: LimitIterator does not end at count -Status: Open +Status: Feedback Type: Bug Package: SPL related Operating System: All PHP Version: Irrelevant Block user comment: N Private report: N Previous Comments: ------------------------------------------------------------------------ [2021-03-29 20:19:46] levim@php.net I'm pretty sure the test code is wrong based on your expectations; you are incrementing $count and then checking if the count is greater than 2: yield 0; if (1 > 2) // false yield 1; if (2 > 2) // false yield 2; if (3 > 2) // true, but your limit is 3, meaning that you asked for this! You need to reduce the limit to 2, or increase the comparison to $count > 3. ------------------------------------------------------------------------ [2021-03-29 20:11:29] alexandreparent_dev at outlook dot com I inverted actual and expected results. ------------------------------------------------------------------------ [2021-03-29 20:08:53] alexandreparent_dev at outlook dot com Description: ------------ LimitIterator will try to iterate beyond $count. If its inner iterator never yield another item nor return, the LimitIterator will not limit at the expected count. Test script: --------------- <?php function generator(): iterable { $count = 0; while (true) { yield $count++; echo 'Yielded: ', $count - 1, \PHP_EOL; if ($count > 2) { while(true); } } } foreach (new \LimitIterator($generator(), 0, 3) as $count) { echo 'Iterating: ', $count, \PHP_EOL; } Expected result: ---------------- Iterating: 0 Yielded: 0 Iterating: 1 Yielded: 1 Iterating: 2 Yielded: 2 // Hanging here Actual result: -------------- Iterating: 0 Yielded: 0 Iterating: 1 Yielded: 1 Iterating: 2 // Exiting here ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80917&edit=1

« previous php.bugs (#233056) next »