Skip to content

Commit 08bf354

Browse files
peffgitster
authored andcommitted
untracked-cache: simplify parsing by dropping "len"
The code which parses untracked-cache extensions from disk keeps a "len" variable, which is the size of the string we are parsing. But since we now have an "end of string" variable, we can just use that to get the length when we need it. This eliminates the need to keep "len" up to date (and removes the possibility of any errors where "len" and "eos" get out of sync). As a bonus, it means we are not storing a string length in an "int", which is a potential source of overflows (though in this case it seems fairly unlikely for that to cause any memory problems). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b511d6d commit 08bf354

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

dir.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,7 +2735,7 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
27352735
const unsigned char *data = rd->data, *end = rd->end;
27362736
const unsigned char *eos;
27372737
unsigned int value;
2738-
int i, len;
2738+
int i;
27392739

27402740
memset(&ud, 0, sizeof(ud));
27412741

@@ -2756,28 +2756,25 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
27562756
eos = memchr(data, '\0', end - data);
27572757
if (!eos || eos == end)
27582758
return -1;
2759-
len = eos - data;
27602759

2761-
*untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), len, 1));
2760+
*untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1));
27622761
memcpy(untracked, &ud, sizeof(ud));
2763-
memcpy(untracked->name, data, len + 1);
2762+
memcpy(untracked->name, data, eos - data + 1);
27642763
data = eos + 1;
27652764

27662765
for (i = 0; i < untracked->untracked_nr; i++) {
27672766
eos = memchr(data, '\0', end - data);
27682767
if (!eos || eos == end)
27692768
return -1;
2770-
len = eos - data;
2771-
untracked->untracked[i] = xmemdupz(data, len);
2769+
untracked->untracked[i] = xmemdupz(data, eos - data);
27722770
data = eos + 1;
27732771
}
27742772

27752773
rd->ucd[rd->index++] = untracked;
27762774
rd->data = data;
27772775

27782776
for (i = 0; i < untracked->dirs_nr; i++) {
2779-
len = read_one_dir(untracked->dirs + i, rd);
2780-
if (len < 0)
2777+
if (read_one_dir(untracked->dirs + i, rd) < 0)
27812778
return -1;
27822779
}
27832780
return 0;

0 commit comments

Comments
 (0)