Skip to content

Commit b26cb7c

Browse files
peffgitster
authored andcommitted
receive-pack: simplify keep_arg computation
To generate "--keep=receive-pack $pid on $host", we write progressively into a single buffer, which requires keeping track of how much we've written so far. But since the result is destined to go into our argv array, we can simply use argv_array_pushf. Unfortunately we still have to have a fixed-size buffer for the gethostname() call, but at least it now doesn't involve any extra size computation. And as a bonus, we drop an sprintf and a strcpy call. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c7ab0ba commit b26cb7c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

builtin/receive-pack.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,15 +1524,18 @@ static const char *unpack(int err_fd, struct shallow_info *si)
15241524
if (status)
15251525
return "unpack-objects abnormal exit";
15261526
} else {
1527-
int s;
1528-
char keep_arg[256];
1529-
1530-
s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
1531-
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
1532-
strcpy(keep_arg + s, "localhost");
1527+
char hostname[256];
15331528

15341529
argv_array_pushl(&child.args, "index-pack",
1535-
"--stdin", hdr_arg, keep_arg, NULL);
1530+
"--stdin", hdr_arg, NULL);
1531+
1532+
if (gethostname(hostname, sizeof(hostname)))
1533+
xsnprintf(hostname, sizeof(hostname), "localhost");
1534+
argv_array_pushf(&child.args,
1535+
"--keep=receive-pack %"PRIuMAX" on %s",
1536+
(uintmax_t)getpid(),
1537+
hostname);
1538+
15361539
if (fsck_objects)
15371540
argv_array_pushf(&child.args, "--strict%s",
15381541
fsck_msg_types.buf);

0 commit comments

Comments
 (0)