Merge pull request #10419 from yuwata/fix-prioq
Fix segfault in prioq_remove() with empty Prioq object
This commit is contained in:
+10
-3
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user