]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: fix segfault in prioq_remove() with empty Prioq object
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 16 Oct 2018 13:27:30 +0000 (22:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 16 Oct 2018 13:27:30 +0000 (22:27 +0900)
src/basic/prioq.c
src/test/test-prioq.c

index ef28a086d149ad48e090974e4435b91faec1d046..e816b2c62a5cf6ffd83ceeef4e910a99e25ac57c 100644 (file)
@@ -211,6 +211,9 @@ _pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx)
 
         assert(q);
 
+        if (q->n_items <= 0)
+                return NULL;
+
         if (idx) {
                 if (*idx == PRIOQ_IDX_NULL ||
                     *idx > q->n_items)
index 8aff66978e5f8c88c9b5f2c3d3a1d16a8906a41b..b2ee70bca6a595fdb938be49d170ee7864cd6c1f 100644 (file)
@@ -86,6 +86,7 @@ static void test_struct(void) {
         while ((t = set_steal_first(s))) {
                 assert_se(prioq_remove(q, t, &t->idx) == 1);
                 assert_se(prioq_remove(q, t, &t->idx) == 0);
+                assert_se(prioq_remove(q, t, NULL) == 0);
 
                 free(t);
         }
@@ -94,6 +95,8 @@ static void test_struct(void) {
                 assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
 
                 assert_se(t = prioq_pop(q));
+                assert_se(prioq_remove(q, t, &t->idx) == 0);
+                assert_se(prioq_remove(q, t, NULL) == 0);
                 assert_se(previous <= t->value);
 
                 previous = t->value;