From c5183ea57fc75205ea9d363cac4c5d6e4bb7cf38 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Mon, 28 Mar 2022 12:39:12 -0400 Subject: [PATCH] Heap code fix When deleting an entry from a heap that was at entry h->used - 1, we'd end up calling heap_sift() on an entry outside the heap - the entry we just removed - which would end up re-adding it to the heap and deleting something we didn't want to delete. Oops... Signed-off-by: Kent Overstreet --- linux/timer.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/linux/timer.c b/linux/timer.c index eb937863..7d519a4d 100644 --- a/linux/timer.c +++ b/linux/timer.c @@ -93,9 +93,11 @@ do { \ \ BUG_ON(_i >= (h)->used); \ (h)->used--; \ - heap_swap(h, _i, (h)->used); \ - heap_sift_down(h, _i, cmp); \ - heap_sift(h, _i, cmp); \ + if ((_i) < (h)->used) { \ + heap_swap(h, _i, (h)->used); \ + heap_sift_down(h, _i, cmp); \ + heap_sift(h, _i, cmp); \ + } \ } while (0) #define heap_pop(h, d, cmp) \