Skip to content

Commit 9568963

Browse files
committed
cache-tree: use parse_int_from_buf()
In c4c9089 (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()).
1 parent 0e6097f commit 9568963

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

cache-tree.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "promisor-remote.h"
1717
#include "trace.h"
1818
#include "trace2.h"
19+
#include "parse.h"
1920

2021
#ifndef DEBUG_CACHE_TREE
2122
#define DEBUG_CACHE_TREE 0
@@ -550,32 +551,13 @@ void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
550551

551552
static int parse_int(const char **ptr, unsigned long *len_p, int *out)
552553
{
553-
const char *s = *ptr;
554-
unsigned long len = *len_p;
555-
int ret = 0;
556-
int sign = 1;
557-
558-
while (len && *s == '-') {
559-
sign *= -1;
560-
s++;
561-
len--;
562-
}
563-
564-
while (len) {
565-
if (!isdigit(*s))
566-
break;
567-
ret *= 10;
568-
ret += *s - '0';
569-
s++;
570-
len--;
571-
}
554+
const char *ep;
572555

573-
if (s == *ptr)
556+
if (!parse_int_from_buf(*ptr, *len_p, &ep, out))
574557
return -1;
575558

576-
*ptr = s;
577-
*len_p = len;
578-
*out = sign * ret;
559+
*len_p -= ep - *ptr;
560+
*ptr = ep;
579561
return 0;
580562
}
581563

0 commit comments

Comments
 (0)