path: root/builtin/receive-pack.c
diff options
authorJeff King <>2015-09-24 21:07:00 (GMT)
committerJunio C Hamano <>2015-09-25 17:18:18 (GMT)
commitb7115a350b5c01ce0ae7a8735e4235d4b2367b5f (patch)
treec61ee7939864a8d3a82abcc26fe803851599f0f4 /builtin/receive-pack.c
parent0cc41428596ec1cd3862918ef781793ef7346ba5 (diff)
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 <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/receive-pack.c')
1 files changed, 2 insertions, 2 deletions
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index e6b93d0..04d2bdf 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -280,10 +280,10 @@ static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2
static void report_message(const char *prefix, const char *err, va_list params)
- int sz = strlen(prefix);
+ int sz;
char msg[4096];
- strncpy(msg, prefix, sz);
+ sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
if (sz > (sizeof(msg) - 1))
sz = sizeof(msg) - 1;