Skip to content

Commit b9ab810

Browse files
Michael Schubertgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Michael Schubert <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f696543 commit b9ab810

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

builtin/patch-id.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
5656
return 1;
5757
}
5858

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)
6060
{
61-
static char line[1000];
6261
int patchlen = 0, found_next = 0;
6362
int before = -1, after = -1;
6463

65-
while (fgets(line, sizeof(line), stdin) != NULL) {
64+
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
65+
char *line = line_buf->buf;
6666
char *p = line;
6767
int len;
6868

@@ -133,14 +133,16 @@ static void generate_id_list(void)
133133
unsigned char sha1[20], n[20];
134134
git_SHA_CTX ctx;
135135
int patchlen;
136+
struct strbuf line_buf = STRBUF_INIT;
136137

137138
git_SHA1_Init(&ctx);
138139
hashclr(sha1);
139140
while (!feof(stdin)) {
140-
patchlen = get_one_patchid(n, &ctx);
141+
patchlen = get_one_patchid(n, &ctx, &line_buf);
141142
flush_current_id(patchlen, sha1, &ctx);
142143
hashcpy(sha1, n);
143144
}
145+
strbuf_release(&line_buf);
144146
}
145147

146148
static const char patch_id_usage[] = "git patch-id < patch";

0 commit comments

Comments
 (0)