From 6b511b965a1e4f10641335492ffc46360a5c6b53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Mon, 10 Aug 2020 14:26:59 +0200 Subject: [PATCH] lib/generic/queue: fix a bug Emptying the queue and using it again... didn't work :-( Fortunately, no use case in kresd so far could trigger this, I believe: - struct session::waiting is a list of tasks waiting while connection is being established - the temporary queue in session_tasklist_finalize_expired() is also only once filled and emptied --- lib/generic/queue.h | 7 +++++++ lib/generic/test_queue.c | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/lib/generic/queue.h b/lib/generic/queue.h index 10cac8973..e538b81af 100644 --- a/lib/generic/queue.h +++ b/lib/generic/queue.h @@ -198,6 +198,13 @@ static inline void queue_pop_impl(struct queue *q) assert(h && h->end > h->begin); if (h->end - h->begin == 1) { /* removing the last element in the chunk */ + assert((q->len == 1) == (q->head == q->tail)); + if (q->len == 1) { + q->tail = NULL; + assert(!h->next); + } else { + assert(h->next); + } q->head = h->next; free(h); } else { diff --git a/lib/generic/test_queue.c b/lib/generic/test_queue.c index 30300a832..3a6b5be96 100644 --- a/lib/generic/test_queue.c +++ b/lib/generic/test_queue.c @@ -14,6 +14,12 @@ static void test_int(void **state_) queue_int_t q; queue_init(q); + /* Case of emptying the queue (and using again) has been broken for a long time. */ + queue_push(q, 2); + queue_pop(q); + queue_push(q, 4); + queue_pop(q); + queue_push_head(q, 2); queue_push_head(q, 1); queue_push_head(q, 0); -- 2.47.2