uloop: fix uloop_run_timeout
authorFelix Fietkau <nbd@nbd.name>
Tue, 23 May 2023 13:09:33 +0000 (15:09 +0200)
committerFelix Fietkau <nbd@nbd.name>
Tue, 23 May 2023 13:32:36 +0000 (15:32 +0200)
Avoid running infinite poll loop, fix timeout value

Signed-off-by: Felix Fietkau <nbd@nbd.name>
uloop.c

diff --git a/uloop.c b/uloop.c
index 0134fa4f21f55688c1c9a04d05b3afb5e55a1072..d3135d69c51c4520a3c004015ddb7d874f608ddb 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -557,8 +557,7 @@ int uloop_run_timeout(int timeout)
 
        uloop_status = 0;
        uloop_cancelled = false;
-       while (!uloop_cancelled)
-       {
+       do {
                uloop_gettime(&tv);
                uloop_process_timeouts(&tv);
 
@@ -571,10 +570,10 @@ int uloop_run_timeout(int timeout)
                uloop_gettime(&tv);
 
                next_time = uloop_get_next_timeout(&tv);
-               if (timeout >= 0 && timeout < next_time)
-                       next_time = timeout;
+               if (timeout >= 0 && (next_time < 0 || timeout < next_time))
+                               next_time = timeout;
                uloop_run_events(next_time);
-       }
+       } while (!uloop_cancelled && timeout < 0);
 
        --uloop_run_depth;