:: commit e4c3fe1526ab1c72117e3d6eb6e93f20d464f555

Mintsuki <mintsuki@protonmail.com> — 2026-01-11 21:02

parents: 8ca033c966

misc: Add overflow detection to strtoui()

diff --git a/common/lib/misc.s2.c b/common/lib/misc.s2.c
index da5c7e8a..c4b3646b 100644
--- a/common/lib/misc.s2.c
+++ b/common/lib/misc.s2.c
@@ -38,7 +38,17 @@ uint64_t strtoui(const char *s, const char **end, int base) {
                 *end = &s[i];
             break;
         }
-        n = n * base + d;
+        uint64_t mul_result;
+        if (__builtin_mul_overflow(n, (uint64_t)base, &mul_result)) {
+            if (end != NULL)
+                *end = &s[i];
+            return UINT64_MAX;
+        }
+        if (__builtin_add_overflow(mul_result, (uint64_t)d, &n)) {
+            if (end != NULL)
+                *end = &s[i];
+            return UINT64_MAX;
+        }
     }
     return n;
 }
tab: 248 wrap: offon