Skip to content

Commit 5025049

Browse files
peffgitster
authored andcommitted
shortlog: use strbufs to read from stdin
We currently use fixed-size buffers with fgets(), which could lead to incorrect results in the unlikely event that a line had something like "Author:" at exactly its 1024th character. But it's easy to convert this to a strbuf, and because we can reuse the same buffer through the loop, we don't even pay the extra allocation cost. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5c3894c commit 5025049

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

builtin/shortlog.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,24 @@ static void insert_one_record(struct shortlog *log,
9191

9292
static void read_from_stdin(struct shortlog *log)
9393
{
94-
char author[1024], oneline[1024];
94+
struct strbuf author = STRBUF_INIT;
95+
struct strbuf oneline = STRBUF_INIT;
9596

96-
while (fgets(author, sizeof(author), stdin) != NULL) {
97+
while (strbuf_getline(&author, stdin, '\n') != EOF) {
9798
const char *v;
98-
if (!skip_prefix(author, "Author: ", &v) &&
99-
!skip_prefix(author, "author ", &v))
99+
if (!skip_prefix(author.buf, "Author: ", &v) &&
100+
!skip_prefix(author.buf, "author ", &v))
100101
continue;
101-
while (fgets(oneline, sizeof(oneline), stdin) &&
102-
oneline[0] != '\n')
102+
while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
103+
oneline.len)
103104
; /* discard headers */
104-
while (fgets(oneline, sizeof(oneline), stdin) &&
105-
oneline[0] == '\n')
105+
while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
106+
!oneline.len)
106107
; /* discard blanks */
107-
insert_one_record(log, v, oneline);
108+
insert_one_record(log, v, oneline.buf);
108109
}
110+
strbuf_release(&author);
111+
strbuf_release(&oneline);
109112
}
110113

111114
void shortlog_add_commit(struct shortlog *log, struct commit *commit)

0 commit comments

Comments
 (0)