]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
util: fix endless loop in get_backoff_delta_msec
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sun, 29 Jun 2025 11:13:34 +0000 (13:13 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 7 Jul 2025 15:20:33 +0000 (10:20 -0500)
If current time t is already past tend, the while loop in
get_backoff_delta_msec never ends.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/377
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
shared/util.c

index 2ffe5647196e180f228db00d50190401fcd23015..3d5d179bb09ec0055cccf6cba648822462f4a433 100644 (file)
@@ -540,16 +540,23 @@ unsigned long long get_backoff_delta_msec(unsigned long long tend,
 
        t = now_msec();
 
-       if (!*delta)
-               *delta = 1;
-       else
-               *delta <<= 1;
+       if (tend <= t) {
+               /* Timeout already reached */
+               *delta = 0;
+       } else {
+               const unsigned long long limit = tend - t;
+
+               if (!*delta)
+                       *delta = 1;
+               else
+                       *delta <<= 1;
 
-       while (t + *delta > tend)
-               *delta >>= 1;
+               while (*delta > limit)
+                       *delta >>= 1;
 
-       if (!*delta && tend > t)
-               *delta = tend - t;
+               if (!*delta)
+                       *delta = limit;
+       }
 
        return t + *delta;
 }