]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cache-tree: use parse_int_from_buf()
authorJeff King <peff@peff.net>
Sun, 30 Nov 2025 13:15:51 +0000 (08:15 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sun, 30 Nov 2025 18:03:43 +0000 (10:03 -0800)
In c4c9089584 (cache-tree: avoid strtol() on non-string buffer,
2025-11-18) we wrote an ad-hoc integer parser which did not detect
overflow. This wasn't too big a problem, since the original use of
strtol() did not do so either. But now that we have a more robust
parsing function, let's use that. It reduces the amount of code and
should catch more cases of malformed entries.

I kept our local parse_int() wrapper here, since it handles management
of our ptr/len pair (rather than doing it inline in the entry parser of
read_one()).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache-tree.c

index 2d8947b51878407200236f0d0e894645f69934ea..f8fb2904437607514d25f217d824a03293ab9ba0 100644 (file)
@@ -16,6 +16,7 @@
 #include "promisor-remote.h"
 #include "trace.h"
 #include "trace2.h"
+#include "parse.h"
 
 #ifndef DEBUG_CACHE_TREE
 #define DEBUG_CACHE_TREE 0
@@ -550,32 +551,13 @@ void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
 
 static int parse_int(const char **ptr, unsigned long *len_p, int *out)
 {
-       const char *s = *ptr;
-       unsigned long len = *len_p;
-       int ret = 0;
-       int sign = 1;
-
-       while (len && *s == '-') {
-               sign *= -1;
-               s++;
-               len--;
-       }
-
-       while (len) {
-               if (!isdigit(*s))
-                       break;
-               ret *= 10;
-               ret += *s - '0';
-               s++;
-               len--;
-       }
+       const char *ep;
 
-       if (s == *ptr)
+       if (!parse_int_from_buf(*ptr, *len_p, &ep, out))
                return -1;
 
-       *ptr = s;
-       *len_p = len;
-       *out = sign * ret;
+       *len_p -= ep - *ptr;
+       *ptr = ep;
        return 0;
 }