From: Elia Pinto Date: Tue, 31 Jan 2017 13:45:35 +0000 (+0000) Subject: builtin/commit.c: switch to strbuf, instead of snprintf() X-Git-Tag: v2.12.0-rc0~11^2 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/4a5281917b2b84affa9942c991419115088aec0e?hp=--cc builtin/commit.c: switch to strbuf, instead of snprintf() Switch to dynamic allocation with strbuf, so we can avoid dealing with magic numbers in the code and reduce the cognitive burden from the programmers. The original code is correct, but programmers no longer have to count bytes needed for static allocation to know that. As a side effect of this change, we also reduce the snprintf() calls, that may silently truncate results if the programmer is not careful. Helped-by: René Scharfe Helped-by: Junio C Hamano Helped-by: Jeff King Signed-off-by: Elia Pinto Signed-off-by: Junio C Hamano --- 4a5281917b2b84affa9942c991419115088aec0e diff --git a/builtin/commit.c b/builtin/commit.c index 276c4f2e7e..2de5f6cc64 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1525,12 +1525,10 @@ static int git_commit_config(const char *k, const char *v, void *cb) static int run_rewrite_hook(const unsigned char *oldsha1, const unsigned char *newsha1) { - /* oldsha1 SP newsha1 LF NUL */ - static char buf[2*40 + 3]; struct child_process proc = CHILD_PROCESS_INIT; const char *argv[3]; int code; - size_t n; + struct strbuf sb = STRBUF_INIT; argv[0] = find_hook("post-rewrite"); if (!argv[0]) @@ -1546,11 +1544,11 @@ static int run_rewrite_hook(const unsigned char *oldsha1, code = start_command(&proc); if (code) return code; - n = snprintf(buf, sizeof(buf), "%s %s\n", - sha1_to_hex(oldsha1), sha1_to_hex(newsha1)); + strbuf_addf(&sb, "%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1)); sigchain_push(SIGPIPE, SIG_IGN); - write_in_full(proc.in, buf, n); + write_in_full(proc.in, sb.buf, sb.len); close(proc.in); + strbuf_release(&sb); sigchain_pop(SIGPIPE); return finish_command(&proc); }