receive-pack: convert strncpy to xsnprintf
This strncpy is pointless; we pass the strlen() of the src string, meaning that it works just like a memcpy. Worse, though, is that the size has no relation to the destination buffer, meaning it is a potential overflow. In practice, it's not. We pass only short constant strings like "warning: " and "error: ", which are much smaller than the destination buffer. We can make this much simpler by just using xsnprintf, which will check for overflow and return the size for our next vsnprintf, without us having to run a separate strlen(). 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
b7115a350b5c01ce0ae7a8735e4235d4b2367b5f
1 file changed
+2
-2
builtin/receive-pack.c
+2
-2
@@ -280,10 +280,10 @@ static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2
280
281
static void report_message(const char *prefix, const char *err, va_list params)
282
{
283
- int sz = strlen(prefix);
283
+ int sz;
284
char msg[4096];
285
286
- strncpy(msg, prefix, sz);
286
+ sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
287
sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
288
if (sz > (sizeof(msg) - 1))
289
sz = sizeof(msg) - 1;