rerere: lift PATH_MAX limitation

The MERGE_RR file records a collection of NUL-terminated entries, each of which consists of - a hash that identifies the conflict - a HT - the pathname We used to read this piece-by-piece, and worse yet, read the pathname part a byte at a time into a fixed buffer of size PATH_MAX. Instead, read a whole entry using strbuf_getwholeline() and parse out the fields. This way, we issue fewer read(2) calls and more importantly we do not have to limit the pathname to PATH_MAX. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 28, 2015 at 15:51 UTC f5800f6ad8b8cbf41a252f7ca0ae465217174c60
1 file changed +15 -20
rerere.c
+15 -20
@@ -35,32 +35,27 @@ static int has_rerere_resolution(const char *hex)
35
36 static void read_rr(struct string_list *rr)
37 {
38 - unsigned char sha1[20];
39 - char buf[PATH_MAX];
38 + struct strbuf buf = STRBUF_INIT;
39 FILE *in = fopen(merge_rr_path, "r");
40 +
41 if (!in)
42 return;
43 - while (fread(buf, 40, 1, in) == 1) {
44 - int i;
45 - char *name;
46 - if (get_sha1_hex(buf, sha1))
43 + while (!strbuf_getwholeline(&buf, in, '\0')) {
44 + char *path;
45 + unsigned char sha1[20];
46 +
47 + /* There has to be the hash, tab, path and then NUL */
48 + if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
49 die("corrupt MERGE_RR");
48 - buf[40] = '\0';
49 - name = xstrdup(buf);
50 - if (fgetc(in) != '\t')
50 +
51 + if (buf.buf[40] != '\t')
52 die("corrupt MERGE_RR");
52 - for (i = 0; i < sizeof(buf); i++) {
53 - int c = fgetc(in);
54 - if (c < 0)
55 - die("corrupt MERGE_RR");
56 - buf[i] = c;
57 - if (c == 0)
58 - break;
59 - }
60 - if (i == sizeof(buf))
61 - die("filename too long");
62 - string_list_insert(rr, buf)->util = name;
53 + buf.buf[40] = '\0';
54 + path = buf.buf + 41;
55 +
56 + string_list_insert(rr, path)->util = xstrdup(buf.buf);
57 }
58 + strbuf_release(&buf);
59 fclose(in);
60 }
61