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 <peff@peff.net>
This commit is contained in:
Jeff King
2017-03-28 15:46:50 -04:00
committed by Junio C Hamano
parent 446d5d9112
commit 5b1ef2cef4
4 changed files with 17 additions and 14 deletions

View File

@ -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