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 <johannes.sixt@telecom.at>
This commit is contained in:
Johannes Sixt
2008-03-28 16:00:17 +01:00
parent 40c020f0d8
commit f32edf6b14

View File

@@ -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)