Merge pull request #10419 from yuwata/fix-prioq

Fix segfault in prioq_remove() with empty Prioq object
This commit is contained in:
Lennart Poettering
2018-10-16 19:56:04 +02:00
committed by GitHub
3 changed files with 35 additions and 54 deletions
+10 -3
View File
@@ -32,11 +32,14 @@ struct Prioq {
Prioq *prioq_new(compare_func_t compare_func) {
Prioq *q;
q = new0(Prioq, 1);
q = new(Prioq, 1);
if (!q)
return q;
q->compare_func = compare_func;
*q = (Prioq) {
.compare_func = compare_func,
};
return q;
}
@@ -88,6 +91,7 @@ static void swap(Prioq *q, unsigned j, unsigned k) {
static unsigned shuffle_up(Prioq *q, unsigned idx) {
assert(q);
assert(idx < q->n_items);
while (idx > 0) {
unsigned k;
@@ -211,9 +215,12 @@ _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)
*idx >= q->n_items)
return NULL;
i = q->items + *idx;
+1
View File
@@ -12,6 +12,7 @@ typedef struct Prioq Prioq;
Prioq *prioq_new(compare_func_t compare);
Prioq *prioq_free(Prioq *q);
DEFINE_TRIVIAL_CLEANUP_FUNC(Prioq*, prioq_free);
int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func);
int prioq_put(Prioq *q, void *data, unsigned *idx);