Fix vsnprintf() emulation again.

On Windows, if the native vsnprintf() fills the buffer, it does not add
the terminating NUL. The earlier commit f32edf6b tried to fix the
emulation, but the fix worked only if the formatted string fitted into
the buffer exactly. However, there are callers that do not try to resize
the buffer if it overflows, and in these cases the resulting string
remained without the trailing NUL. This patch adds the NUL in all cases.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
This commit is contained in:
Johannes Sixt
2008-06-11 15:15:08 +02:00
parent b1013825f9
commit 3ec8d36fbf

View File

@@ -13,14 +13,15 @@
int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
{
char *s;
int ret;
int ret = -1;
ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
if (ret != -1) {
/* Windows does not NUL-terminate if result fits exactly */
str[ret] = 0;
return ret;
if (maxsize > 0) {
ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
/* Windows does not NUL-terminate if result fills buffer */
str[maxsize-1] = 0;
}
if (ret != -1)
return ret;
s = NULL;
if (maxsize < 128)