From f32edf6b14ae405b9e7a4842f18990de6bb4a322 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 28 Mar 2008 16:00:17 +0100 Subject: [PATCH] snprintf replacement: Make sure the result is NUL terminated. On Windows, if the resulting string fits exactly in the provided buffer, but not the terminating NUL, then the return value of the system's vsnprintf is the number of characters written. But since we had reserved an extra byte anyway, we only need to make sure that the result is NUL terminated. Signed-off-by: Johannes Sixt --- compat/snprintf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compat/snprintf.c b/compat/snprintf.c index 480b66f94e..bddfa5cdde 100644 --- a/compat/snprintf.c +++ b/compat/snprintf.c @@ -16,8 +16,11 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) int ret; ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap); - if (ret != -1) + if (ret != -1) { + /* Windows does not NUL-terminate if result fits exactly */ + str[ret] = 0; return ret; + } s = NULL; if (maxsize < 128)