patch-id.c: use strbuf instead of a fixed buffer

get_one_patchid() uses a rather dumb heuristic to determine if the passed buffer is part of the next commit. Whenever the first 40 bytes are a valid hexadecimal sha1 representation, get_one_patchid() returns next_sha1. Once the current line is longer than the fixed buffer, this will break (provided the additional bytes make a valid hexadecimal sha1). As a result patch-id returns incorrect results. Instead, use strbuf and read one line at a time. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Michael Schubert <mschub@elegosoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Schubert committed Sep 21, 2011 at 14:42 UTC b9ab810b1826a7757e16f152faf0c28c2fdaa679
1 file changed +6 -4
builtin/patch-id.c
+6 -4
@@ -56,13 +56,13 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
56 return 1;
57 }
58
59 -static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
59 +static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx, struct strbuf *line_buf)
60 {
61 - static char line[1000];
61 int patchlen = 0, found_next = 0;
62 int before = -1, after = -1;
63
65 - while (fgets(line, sizeof(line), stdin) != NULL) {
64 + while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
65 + char *line = line_buf->buf;
66 char *p = line;
67 int len;
68
@@ -133,14 +133,16 @@ static void generate_id_list(void)
133 unsigned char sha1[20], n[20];
134 git_SHA_CTX ctx;
135 int patchlen;
136 + struct strbuf line_buf = STRBUF_INIT;
137
138 git_SHA1_Init(&ctx);
139 hashclr(sha1);
140 while (!feof(stdin)) {
140 - patchlen = get_one_patchid(n, &ctx);
141 + patchlen = get_one_patchid(n, &ctx, &line_buf);
142 flush_current_id(patchlen, sha1, &ctx);
143 hashcpy(sha1, n);
144 }
145 + strbuf_release(&line_buf);
146 }
147
148 static const char patch_id_usage[] = "git patch-id < patch";