Skip to content

Commit b7115a3

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cc4142 commit b7115a3

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

builtin/receive-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2
280280

281281
static void report_message(const char *prefix, const char *err, va_list params)
282282
{
283-
int sz = strlen(prefix);
283+
int sz;
284284
char msg[4096];
285285

286-
strncpy(msg, prefix, sz);
286+
sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
287287
sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
288288
if (sz > (sizeof(msg) - 1))
289289
sz = sizeof(msg) - 1;

0 commit comments

Comments
 (0)