fetch: replace static buffer with xstrfmt

We parse the INFINITE_DEPTH constant into a static, fixed-size buffer using sprintf. This buffer is sufficiently large for the current constant, but it's a suspicious pattern, as the constant is defined far away, and it's not immediately obvious that 12 bytes are large enough to hold it. We can just use xstrfmt here, which gets rid of any question of the buffer size. It also removes any concerns with object lifetime, which means we do not have to wonder why this buffer deep within a conditional is marked "static" (we never free our newly allocated result, of course, but that's OK; it's global that lasts the lifetime of the whole program anyway). 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:07 UTC 2805bb59708e1cf049445fb8b21de1a710a3a16c
1 file changed +2 -5
builtin/fetch.c
+2 -5
@@ -1156,11 +1156,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
1156 die(_("--depth and --unshallow cannot be used together"));
1157 else if (!is_repository_shallow())
1158 die(_("--unshallow on a complete repository does not make sense"));
1159 - else {
1160 - static char inf_depth[12];
1161 - sprintf(inf_depth, "%d", INFINITE_DEPTH);
1162 - depth = inf_depth;
1163 - }
1159 + else
1160 + depth = xstrfmt("%d", INFINITE_DEPTH);
1161 }
1162
1163 /* no need to be strict, transport_set_option() will validate it again */