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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 18, 2019 at 17:18 UTC 08bf354de71a806bad319ec236740ac698b58a5b
1 file changed +5 -8
dir.c
+5 -8
@@ -2735,7 +2735,7 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
2735 const unsigned char *data = rd->data, *end = rd->end;
2736 const unsigned char *eos;
2737 unsigned int value;
2738 - int i, len;
2738 + int i;
2739
2740 memset(&ud, 0, sizeof(ud));
2741
@@ -2756,19 +2756,17 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
2756 eos = memchr(data, '\0', end - data);
2757 if (!eos || eos == end)
2758 return -1;
2759 - len = eos - data;
2759
2761 - *untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), len, 1));
2760 + *untracked_ = untracked = xmalloc(st_add3(sizeof(*untracked), eos - data, 1));
2761 memcpy(untracked, &ud, sizeof(ud));
2763 - memcpy(untracked->name, data, len + 1);
2762 + memcpy(untracked->name, data, eos - data + 1);
2763 data = eos + 1;
2764
2765 for (i = 0; i < untracked->untracked_nr; i++) {
2766 eos = memchr(data, '\0', end - data);
2767 if (!eos || eos == end)
2768 return -1;
2770 - len = eos - data;
2771 - untracked->untracked[i] = xmemdupz(data, len);
2769 + untracked->untracked[i] = xmemdupz(data, eos - data);
2770 data = eos + 1;
2771 }
2772
@@ -2776,8 +2774,7 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
2774 rd->data = data;
2775
2776 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)
2778 return -1;
2779 }
2780 return 0;