]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
prioq: invalidate index pointers on removal
authorLennart Poettering <lennart@poettering.net>
Wed, 26 Mar 2025 15:18:40 +0000 (11:18 -0400)
committerLennart Poettering <lennart@poettering.net>
Wed, 2 Apr 2025 15:27:15 +0000 (17:27 +0200)
src/basic/prioq.c

index b3cf88ecad166327177bf01ee71456710e4968fc..194bd5e7eb30480bd71f8fdfd200bbab27c57770 100644 (file)
@@ -46,6 +46,11 @@ Prioq* prioq_free(Prioq *q) {
         if (!q)
                 return NULL;
 
+        /* Invalidate the index fields of any remaining objects */
+        FOREACH_ARRAY(item, q->items, q->n_items)
+                if (item->idx)
+                        *(item->idx) = PRIOQ_IDX_NULL;
+
         free(q->items);
         return mfree(q);
 }
@@ -178,6 +183,11 @@ static void remove_item(Prioq *q, struct prioq_item *i) {
         assert(q);
         assert(i);
 
+        /* Let's invalidate the index pointer stored in the user's object to indicate the item is now removed
+         * from the priority queue */
+        if (i->idx)
+                *(i->idx) = PRIOQ_IDX_NULL;
+
         l = q->items + q->n_items - 1;
 
         if (i == l)