From 5b1ef2cef4ff9d3213ec81465b99affb4a7c8083 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 28 Mar 2017 15:46:50 -0400 Subject: replace unchecked snprintf calls with heap buffers We'd prefer to avoid unchecked snprintf calls because truncation can lead to unexpected results. These are all cases where truncation shouldn't ever happen, because the input to snprintf is fixed in size. That makes them candidates for xsnprintf(), but it's simpler still to just use the heap, and then nobody has to wonder if "100" is big enough. We'll use xstrfmt() where possible, and a strbuf when we need the resulting size or to reuse the same buffer in a loop. Signed-off-by: Jeff King --- builtin/index-pack.c | 9 +++++---- builtin/notes.c | 9 ++++----- builtin/rev-parse.c | 5 +++-- 3 files changed, 12 insertions(+), 11 deletions(-) (limited to 'builtin') diff --git a/builtin/index-pack.c b/builtin/index-pack.c index f4af2ab37a..197c51912d 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1443,10 +1443,11 @@ static void final(const char *final_pack_name, const char *curr_pack_name, if (!from_stdin) { printf("%s\n", sha1_to_hex(sha1)); } else { - char buf[48]; - int len = snprintf(buf, sizeof(buf), "%s\t%s\n", - report, sha1_to_hex(sha1)); - write_or_die(1, buf, len); + struct strbuf buf = STRBUF_INIT; + + strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(sha1)); + write_or_die(1, buf.buf, buf.len); + strbuf_release(&buf); /* * Let's just mimic git-unpack-objects here and write diff --git a/builtin/notes.c b/builtin/notes.c index 0513f7455d..7b891471c4 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -554,7 +554,7 @@ static int append_edit(int argc, const char **argv, const char *prefix) struct notes_tree *t; unsigned char object[20], new_note[20]; const unsigned char *note; - char logmsg[100]; + char *logmsg; const char * const *usage; struct note_data d = { 0, 0, NULL, STRBUF_INIT }; struct option options[] = { @@ -618,17 +618,16 @@ static int append_edit(int argc, const char **argv, const char *prefix) write_note_data(&d, new_note); if (add_note(t, object, new_note, combine_notes_overwrite)) die("BUG: combine_notes_overwrite failed"); - snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'", - argv[0]); + logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]); } else { fprintf(stderr, _("Removing note for object %s\n"), sha1_to_hex(object)); remove_note(t, object); - snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'", - argv[0]); + logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]); } commit_notes(t, logmsg); + free(logmsg); free_note_data(&d); free_notes(t); return 0; diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 9e53a1a7ca..f54d7b5028 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -213,13 +213,14 @@ static int show_abbrev(const unsigned char *sha1, void *cb_data) static void show_datestring(const char *flag, const char *datestr) { - static char buffer[100]; + char *buffer; /* date handling requires both flags and revs */ if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) return; - snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); + buffer = xstrfmt("%s%lu", flag, approxidate(datestr)); show(buffer); + free(buffer); } static int show_file(const char *arg, int output_prefix) -- cgit v1.2.3