Collections-C icon indicating copy to clipboard operation
Collections-C copied to clipboard

invalid access in priority queue

Open coleifer opened this issue 5 years ago • 0 comments

This fixes an out-of-bounds lookup, which results in a segfault when popping/heapifying:

diff --git a/src/pqueue.c b/src/pqueue.c
index 0293922..875d40c 100644
--- a/src/pqueue.c
+++ b/src/pqueue.c
@@ -306,13 +306,13 @@ static void pqueue_heapify(PQueue *pq, size_t index)
     size_t R   = CC_RIGHT(index);
     size_t tmp = index;
 
+    if (L >= pq->size || R >= pq->size)
+        return;
+
     void *left     = pq->buffer[L];
     void *right    = pq->buffer[R];
     void *indexPtr = pq->buffer[index];
 
-    if (L >= pq->size || R >= pq->size)
-        return;
-
     if (pq->cmp(indexPtr, left) < 0) {
         indexPtr = left;
         index = L;

coleifer avatar Jul 22 '19 15:07 coleifer