]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add helper to convert a string to a uint64 number
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 20 Oct 2021 09:38:34 +0000 (10:38 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 20 Oct 2021 09:38:34 +0000 (10:38 +0100)
src/libutil/str_util.c
src/libutil/str_util.h

index fc53a8711c6f38da9b6f12faf29c1cf3d17099fa..6cee32243735dec12f29a55aaf7e638f1a481534 100644 (file)
@@ -526,6 +526,28 @@ rspamd_strtol (const gchar *s, gsize len, glong *value)
 /*
  * Try to convert string of length to long
  */
+#define CONV_STR_LIM_DECIMAL(max_num) do { \
+    while (p < end) { \
+        c = *p; \
+        if (c >= '0' && c <= '9') { \
+            c -= '0'; \
+            if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) { \
+                *value = (max_num); \
+                return FALSE; \
+            } \
+            else { \
+                v *= 10; \
+                v += c; \
+            } \
+        } \
+        else { \
+            *value = v; \
+            return FALSE; \
+        } \
+    p++; \
+    } \
+} while(0)
+
 gboolean
 rspamd_strtoul (const gchar *s, gsize len, gulong *value)
 {
@@ -535,27 +557,22 @@ rspamd_strtoul (const gchar *s, gsize len, gulong *value)
        const gulong cutoff = G_MAXULONG / 10, cutlim = G_MAXULONG % 10;
 
        /* Some preparations for range errors */
-       while (p < end) {
-               c = *p;
-               if (c >= '0' && c <= '9') {
-                       c -= '0';
-                       if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) {
-                               /* Range error */
-                               *value = G_MAXULONG;
-                               return FALSE;
-                       }
-                       else {
-                               v *= 10;
-                               v += c;
-                       }
-               }
-               else {
-                       *value = v;
+       CONV_STR_LIM_DECIMAL(G_MAXULONG);
 
-                       return FALSE;
-               }
-               p++;
-       }
+       *value = v;
+       return TRUE;
+}
+
+gboolean
+rspamd_strtou64 (const gchar *s, gsize len, guint64 *value)
+{
+       const gchar *p = s, *end = s + len;
+       gchar c;
+       guint64 v = 0;
+       const guint64 cutoff = G_MAXUINT64 / 10, cutlim = G_MAXUINT64 % 10;
+
+       /* Some preparations for range errors */
+       CONV_STR_LIM_DECIMAL(G_MAXUINT64);
 
        *value = v;
        return TRUE;
index e5e4cfb7662fb7857d2bc5ffcd47fd7f706b8854..b08dd56ad8dc856c5d29112cfe4cfb84a7251153 100644 (file)
@@ -141,6 +141,7 @@ gboolean rspamd_strtol (const gchar *s, gsize len, glong *value);
  * Try to convert a string of length to unsigned long
  */
 gboolean rspamd_strtoul (const gchar *s, gsize len, gulong *value);
+gboolean rspamd_strtou64 (const gchar *s, gsize len, guint64 *value);
 
 /*
  * Try to convert a hex string of length to unsigned long