OSDN Git Service

compat/snprintf.c: handle snprintf's that always return the # chars transmitted
authorBrandon Casey <casey@nrlssc.navy.mil>
Thu, 21 Aug 2008 01:53:50 +0000 (20:53 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 21 Aug 2008 08:53:44 +0000 (01:53 -0700)
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.

IRIX 6.5 is such a platform. IRIX does have a working snprintf(), but it
is only provided when _NO_XOPEN5 evaluates to zero, and this only happens
if _XOPEN_SOURCE is defined, but definition of _XOPEN_SOURCE prevents
inclusion of many other common functions and defines. So it must be avoided.

Work around these horribly broken snprintf implementations by detecting an
snprintf call which results in the number of transmitted characters exactly
equal to the length of our buffer and retrying with a larger buffer just to
be safe.

Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Acked-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/snprintf.c

index 580966e..357e733 100644 (file)
@@ -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;