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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 24, 2015 at 17:08 UTC
b26cb7c777f8fcca5ec26cc6a7f9eba85378104f
1 file changed
+10
-7
builtin/receive-pack.c
+10
-7
@@ -1524,15 +1524,18 @@ static const char *unpack(int err_fd, struct shallow_info *si)
1524
if (status)
1525
return "unpack-objects abnormal exit";
1526
} 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];
1528
1529
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
+
1539
if (fsck_objects)
1540
argv_array_pushf(&child.args, "--strict%s",
1541
fsck_msg_types.buf);