From: Jeff King Date: Sun, 30 Nov 2025 13:15:51 +0000 (-0500) Subject: cache-tree: use parse_int_from_buf() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7cf9c9664648848567bbaebb99ba31b878e3c0e6;p=thirdparty%2Fgit.git cache-tree: use parse_int_from_buf() 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 Signed-off-by: Junio C Hamano --- diff --git a/cache-tree.c b/cache-tree.c index 2d8947b518..f8fb290443 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -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; }