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>
#include "promisor-remote.h"
#include "trace.h"
#include "trace2.h"
+#include "parse.h"
#ifndef DEBUG_CACHE_TREE
#define DEBUG_CACHE_TREE 0
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;
}