From f43ad85eddf708bb1c1d87cd6a1e7e60a9191dd6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 25 Oct 2012 04:26:56 +0200 Subject: [PATCH] include: fix ngx_queue_foreach() macro Guard against the possibility that the queue is emptied while we're iterating over it. Simple test case: #include "ngx-queue.h" #include int main(void) { ngx_queue_t h; ngx_queue_t v[2]; ngx_queue_t* q; unsigned n = 0; ngx_queue_init(&h); ngx_queue_insert_tail(&h, v + 0); ngx_queue_insert_tail(&h, v + 1); ngx_queue_foreach(q, &h) { ngx_queue_remove(v + 0); ngx_queue_remove(v + 1); n++; } assert(n == 1); // *not* 2 return 0; } Fixes #605. --- include/uv-private/ngx-queue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/uv-private/ngx-queue.h b/include/uv-private/ngx-queue.h index 6fd0071ff..08fd655a1 100644 --- a/include/uv-private/ngx-queue.h +++ b/include/uv-private/ngx-queue.h @@ -101,7 +101,7 @@ struct ngx_queue_s { #define ngx_queue_foreach(q, h) \ for ((q) = ngx_queue_head(h); \ - (q) != ngx_queue_sentinel(h); \ + (q) != ngx_queue_sentinel(h) && !ngx_queue_empty(h); \ (q) = ngx_queue_next(q))