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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jan 18, 2016 at 15:02 UTC
50250491bded3190e16978e836a4dbe129c632cf
1 file changed
+12
-9
builtin/shortlog.c
+12
-9
@@ -91,21 +91,24 @@ static void insert_one_record(struct shortlog *log,
91
92
static void read_from_stdin(struct shortlog *log)
93
{
94
- char author[1024], oneline[1024];
94
+ struct strbuf author = STRBUF_INIT;
95
+ struct strbuf oneline = STRBUF_INIT;
96
96
- while (fgets(author, sizeof(author), stdin) != NULL) {
97
+ while (strbuf_getline(&author, stdin, '\n') != EOF) {
98
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))
101
continue;
101
- while (fgets(oneline, sizeof(oneline), stdin) &&
102
- oneline[0] != '\n')
102
+ while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
103
+ oneline.len)
104
; /* discard headers */
104
- while (fgets(oneline, sizeof(oneline), stdin) &&
105
- oneline[0] == '\n')
105
+ while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
106
+ !oneline.len)
107
; /* discard blanks */
107
- insert_one_record(log, v, oneline);
108
+ insert_one_record(log, v, oneline.buf);
109
}
110
+ strbuf_release(&author);
111
+ strbuf_release(&oneline);
112
}
113
114
void shortlog_add_commit(struct shortlog *log, struct commit *commit)