diff mbox series

[libdrm,v2,2/2] include: Avoid potentially infinite loop in log2_int().

Message ID 20201030130612.590543-2-pgofman@codeweavers.com (mailing list archive)
State New, archived
Headers show
Series [libdrm,v2,1/2] include: Factor out log2_int() function. | expand

Commit Message

Paul Gofman Oct. 30, 2020, 1:06 p.m. UTC
Signed-off-by: Paul Gofman <pgofman@codeweavers.com>
---
v2:
    - simplify log2_int implementation.

 util_math.h | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/util_math.h b/util_math.h
index e2fa95f5..32297349 100644
--- a/util_math.h
+++ b/util_math.h
@@ -33,16 +33,11 @@ 
 
 static inline unsigned log2_int(unsigned x)
 {
-    unsigned l;
+    unsigned l = 0;
 
-    if (x < 2) {
-        return 0;
-    }
-    for (l = 2; ; l++) {
-        if ((unsigned)(1 << l) > x) {
-            return l - 1;
-        }
-    }
-    return 0;
+    while (x >>= 1)
+      ++l;
+
+    return l;
 }
 #endif /*_UTIL_MATH_H_*/