Browse Source

lib:Prevent using a removed last entry when re-balancing the tree
In quagga 1.2.4, the code in pqueue_remove_at() in file lib/thread.c tries to re-balance
the queue tree when removing one entry by copying the last entry to the position of the
removed entry before starting the re-balancing process.

This has some problems when the last element is that which is removed. Copying the last element to
the position of the removed entry doesn't hurt in this case, it's just some sort of no-op.
But using this removed entry to start the re-balancing seems wrong.

Fixes bug #982

Robert Vogelgesang 11 months ago
parent
commit
e21719d8fa
1 changed files with 4 additions and 1 deletions
  1. 4 1
      lib/pqueue.c

+ 4 - 1
lib/pqueue.c

@@ -172,7 +172,10 @@ pqueue_dequeue (struct pqueue *queue)
 void
 pqueue_remove_at (int index, struct pqueue *queue)
 {
-  queue->array[index] = queue->array[--queue->size];
+  if (index == --queue->size)
+    return; /* we're removing the last entry */
+
+  queue->array[index] = queue->array[queue->size];
 
   if (index > 0
       && (*queue->cmp) (queue->array[index],