X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Fprioq.c;h=537befc623a1554bd4b57bb115ca223bacddbbb4;hp=b7d745990c4bcf78236ba44d6ad2495766d7a15d;hb=68313d3dfa2082dae8a06643d639e0200afc19fc;hpb=e3017af97310da024ffb378ed155bc1676922ce7 diff --git a/src/shared/prioq.c b/src/shared/prioq.c index b7d745990..537befc62 100644 --- a/src/shared/prioq.c +++ b/src/shared/prioq.c @@ -159,7 +159,7 @@ int prioq_put(Prioq *q, void *data, unsigned *idx) { unsigned n; struct prioq_item *j; - n = MAX((q->n_items+1) * 2, 16); + n = MAX((q->n_items+1) * 2, 16u); j = realloc(q->items, sizeof(struct prioq_item) * n); if (!j) return -ENOMEM; @@ -211,13 +211,14 @@ static void remove_item(Prioq *q, struct prioq_item *i) { } } -static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { +_pure_ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { struct prioq_item *i; assert(q); if (idx) { - if (*idx > q->n_items) + if (*idx == PRIOQ_IDX_NULL || + *idx > q->n_items) return NULL; i = q->items + *idx; @@ -236,7 +237,8 @@ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) { int prioq_remove(Prioq *q, void *data, unsigned *idx) { struct prioq_item *i; - assert(q); + if (!q) + return 0; i = find_item(q, data, idx); if (!i) @@ -263,7 +265,9 @@ int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) { } void *prioq_peek(Prioq *q) { - assert(q); + + if (!q) + return NULL; if (q->n_items <= 0) return NULL; @@ -274,7 +278,8 @@ void *prioq_peek(Prioq *q) { void *prioq_pop(Prioq *q) { void *data; - assert(q); + if (!q) + return NULL; if (q->n_items <= 0) return NULL; @@ -285,13 +290,17 @@ void *prioq_pop(Prioq *q) { } unsigned prioq_size(Prioq *q) { - assert(q); + + if (!q) + return 0; return q->n_items; } bool prioq_isempty(Prioq *q) { - assert(q); + + if (!q) + return true; return q->n_items <= 0; }