Re: [PATCH] compat/snprintf.c: handle snprintf's that always return the # chars transmitted
From: Johannes Sixt <hidden>
Date: 2016-06-15 22:45:12
Brandon Casey schrieb:
Some platforms provide a horribly broken snprintf. More broken than the platforms that return -1 when there is too little space in the target buffer for the formatted string. Some platforms provide an snprintf which _always_ returns the number of characters transmitted to the buffer, regardless of whether there was enough space or not.
...
quoted hunk ↗ jump to hunk
diff --git a/compat/snprintf.c b/compat/snprintf.c index 580966e..357e733 100644 --- a/compat/snprintf.c +++ b/compat/snprintf.c@@ -17,6 +17,8 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) if (maxsize > 0) { ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap); + if (ret == maxsize-1) + ret = -1; /* Windows does not NUL-terminate if result fills buffer */ str[maxsize-1] = 0; }@@ -34,6 +36,8 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap) break; s = str; ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap); + if (ret == maxsize-1) + ret = -1; } free(s); return ret;
This looks good, and it passes the test suite on MinGW. Hence: Acked-by: Johannes Sixt <redacted> -- Hannes