Skip to content

Commit f5800f6

Browse files
committed
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 <[email protected]>
1 parent 8d9b5a4 commit f5800f6

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

rerere.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,27 @@ static int has_rerere_resolution(const char *hex)
3535

3636
static void read_rr(struct string_list *rr)
3737
{
38-
unsigned char sha1[20];
39-
char buf[PATH_MAX];
38+
struct strbuf buf = STRBUF_INIT;
4039
FILE *in = fopen(merge_rr_path, "r");
40+
4141
if (!in)
4242
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))
4749
die("corrupt MERGE_RR");
48-
buf[40] = '\0';
49-
name = xstrdup(buf);
50-
if (fgetc(in) != '\t')
50+
51+
if (buf.buf[40] != '\t')
5152
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);
6357
}
58+
strbuf_release(&buf);
6459
fclose(in);
6560
}
6661

0 commit comments

Comments
 (0)