8000 Fix issue with infinite loop · piowin/zend-stdlib@aae5505 · GitHub
[go: up one dir, main page]

Skip to content

Commit aae5505

Browse files
committed
Fix issue with infinite loop
Inserting elements at 0 priority causes an infinite loop. For example, when we run the following snippet of code, we fall into an infinite loop: ```php $queue = new FastPriorityQueue(); $queue->insert('a', 0); $queue->insert('b', 1); foreach ($queue as $value) {echo $value;} ```
1 parent 0e66240 commit aae5505

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/FastPriorityQueue.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class FastPriorityQueue implements Iterator, Countable, Serializable
5757
/**
5858
* Max priority
5959
*
60-
* @var integer
60+
* @var integer|null
6161
*/
62-
protected $maxPriority = 0;
62+
protected $maxPriority = null;
6363

6464
/**
6565
* Total number of elements in the queue
@@ -86,7 +86,7 @@ class FastPriorityQueue implements Iterator, Countable, Serializable
8686
* Insert an element in the queue with a specified priority
8787
*
8888
* @param mixed $value
89-
* @param integer $priority a positive integer
89+
* @param integer $priority
9090
*/
9191
public function insert($value, $priority)
9292
{
@@ -96,7 +96,7 @@ public function insert($value, $priority)
9696
$this->values[$priority][] = $value;
9797
if (! isset($this->priorities[$priority])) {
9898
$this->priorities[$priority] = $priority;
99-
$this->maxPriority = max($priority, $this->maxPriority);
99+
$this->maxPriority = $this->maxPriority === null ? $priority : max($priority, $this->maxPriority);
100100
}
101101
++$this->count;
102102
}
@@ -194,7 +194,7 @@ protected function nextAndRemove()
194194
if (false === next($this->values[$this->maxPriority])) {
195195
unset($this->priorities[$this->maxPriority]);
196196
unset($this->values[$this->maxPriority]);
197-
$this->maxPriority = empty($this->priorities) ? 0 : max($this->priorities);
197+
$this->maxPriority = empty($this->priorities) ? null : max($this->priorities);
198198
$this->subIndex = -1;
199199
}
200200
++$this->index;
@@ -211,7 +211,7 @@ public function next()
211211
if (false === next($this->values[$this->maxPriority])) {
212212
unset($this->subPriorities[$this->maxPriority]);
213213
reset($this->values[$this->maxPriority]);
214-
$this->maxPriority = empty($this->subPriorities) ? 0 : max($this->subPriorities);
214+
$this->maxPriority = empty($this->subPriorities) ? null : max($this->subPriorities);
215215
$this->subIndex = -1;
216216
}
217217
++$this->index;

0 commit comments

Comments
 (0)
0