From f05cb1b46d0c8152ab8eadd6d81bfd13d971fbe5 Mon Sep 17 00:00:00 2001 From: Gwilym Evans Date: Thu, 26 Jun 2014 12:55:01 +1000 Subject: [PATCH 1/2] use a loop instead of recursion in CursorBasedIterator servers with large sets of keys will cause memory and stack exhaustion if recursion is used --- .../Iterator/CursorBasedIterator.php | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/Predis/Collection/Iterator/CursorBasedIterator.php b/lib/Predis/Collection/Iterator/CursorBasedIterator.php index 3368bd77..2d15d956 100644 --- a/lib/Predis/Collection/Iterator/CursorBasedIterator.php +++ b/lib/Predis/Collection/Iterator/CursorBasedIterator.php @@ -165,17 +165,21 @@ abstract class CursorBasedIterator implements Iterator */ public function next() { - if (!$this->elements && $this->fetchmore) { - $this->fetch(); - } + do { + $more = false; - if ($this->elements) { - $this->extractNext(); - } elseif ($this->cursor) { - $this->next(); - } else { - $this->valid = false; - } + if (!$this->elements && $this->fetchmore) { + $this->fetch(); + } + + if ($this->elements) { + $this->extractNext(); + } elseif ($this->cursor) { + $more = true; + } else { + $this->valid = false; + } + } while ($more); } /** From 2d53db7b9b194d519f4851df5f9ee4ad50d1ba05 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Mon, 7 Jul 2014 10:50:17 +0200 Subject: [PATCH 2/2] Rework PR to use "goto" instead of a "while ... do" loop. --- lib/Predis/Collection/Iterator/CursorBasedIterator.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/Predis/Collection/Iterator/CursorBasedIterator.php b/lib/Predis/Collection/Iterator/CursorBasedIterator.php index 2d15d956..32632443 100644 --- a/lib/Predis/Collection/Iterator/CursorBasedIterator.php +++ b/lib/Predis/Collection/Iterator/CursorBasedIterator.php @@ -165,9 +165,7 @@ abstract class CursorBasedIterator implements Iterator */ public function next() { - do { - $more = false; - + tryFetch: { if (!$this->elements && $this->fetchmore) { $this->fetch(); } @@ -175,11 +173,11 @@ abstract class CursorBasedIterator implements Iterator if ($this->elements) { $this->extractNext(); } elseif ($this->cursor) { - $more = true; + goto tryFetch; } else { $this->valid = false; } - } while ($more); + } } /**