summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-01-18 20:02:44 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-01-19 17:53:08 (GMT)
commit50250491bded3190e16978e836a4dbe129c632cf (patch)
treea6c3b618bed2c01a503aa0a9b75bd053a8f0f564
parent5c3894c39d4095e6875376a6c05c6390b9a50754 (diff)
downloadgit-50250491bded3190e16978e836a4dbe129c632cf.zip
git-50250491bded3190e16978e836a4dbe129c632cf.tar.gz
git-50250491bded3190e16978e836a4dbe129c632cf.tar.bz2
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>
-rw-r--r--builtin/shortlog.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index ab25b44..6c0a72e 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -91,21 +91,24 @@ static void insert_one_record(struct shortlog *log,
static void read_from_stdin(struct shortlog *log)
{
- char author[1024], oneline[1024];
+ struct strbuf author = STRBUF_INIT;
+ struct strbuf oneline = STRBUF_INIT;
- while (fgets(author, sizeof(author), stdin) != NULL) {
+ while (strbuf_getline(&author, stdin, '\n') != EOF) {
const char *v;
- if (!skip_prefix(author, "Author: ", &v) &&
- !skip_prefix(author, "author ", &v))
+ if (!skip_prefix(author.buf, "Author: ", &v) &&
+ !skip_prefix(author.buf, "author ", &v))
continue;
- while (fgets(oneline, sizeof(oneline), stdin) &&
- oneline[0] != '\n')
+ while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
+ oneline.len)
; /* discard headers */
- while (fgets(oneline, sizeof(oneline), stdin) &&
- oneline[0] == '\n')
+ while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
+ !oneline.len)
; /* discard blanks */
- insert_one_record(log, v, oneline);
+ insert_one_record(log, v, oneline.buf);
}
+ strbuf_release(&author);
+ strbuf_release(&oneline);
}
void shortlog_add_commit(struct shortlog *log, struct commit *commit)