]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
Heap code fix
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 28 Mar 2022 16:39:12 +0000 (12:39 -0400)
committerKent Overstreet <kent.overstreet@gmail.com>
Mon, 28 Mar 2022 16:39:12 +0000 (12:39 -0400)
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 <kent.overstreet@gmail.com>
linux/timer.c

index eb93786364eedd6282fa113b3458ecdd8c5bf9b6..7d519a4d85830108b8b0166ada31222d5fe6e59a 100644 (file)
@@ -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)                                            \