[PATCH 3/4] cache-tree: use parse_int_from_buf()
- From
Jeff King <peff@peff.net>
- Date
- Nov 30, 2025, 13:15 UTC
- Message-ID
- <20251130131551.GC199335@coredump.intra.peff.net>
- In-Reply-To
- <20251130131351.GA198697@coredump.intra.peff.net>
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> --- cache-tree.c | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-)
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; }
-- 2.52.0.413.gf695cdb9bd