Skip to content

Commit 4a52819

Browse files
devzero2000gitster
authored andcommitted
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 <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Elia Pinto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d7aa4b commit 4a52819

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

builtin/commit.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,12 +1525,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
15251525
static int run_rewrite_hook(const unsigned char *oldsha1,
15261526
const unsigned char *newsha1)
15271527
{
1528-
/* oldsha1 SP newsha1 LF NUL */
1529-
static char buf[2*40 + 3];
15301528
struct child_process proc = CHILD_PROCESS_INIT;
15311529
const char *argv[3];
15321530
int code;
1533-
size_t n;
1531+
struct strbuf sb = STRBUF_INIT;
15341532

15351533
argv[0] = find_hook("post-rewrite");
15361534
if (!argv[0])
@@ -1546,11 +1544,11 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
15461544
code = start_command(&proc);
15471545
if (code)
15481546
return code;
1549-
n = snprintf(buf, sizeof(buf), "%s %s\n",
1550-
sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1547+
strbuf_addf(&sb, "%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
15511548
sigchain_push(SIGPIPE, SIG_IGN);
1552-
write_in_full(proc.in, buf, n);
1549+
write_in_full(proc.in, sb.buf, sb.len);
15531550
close(proc.in);
1551+
strbuf_release(&sb);
15541552
sigchain_pop(SIGPIPE);
15551553
return finish_command(&proc);
15561554
}

0 commit comments

Comments
 (0)