Browse Source

Fix vasprintf fallback

Once a va_list has been processed, it cannot be used again. Make a
copy and use it for the initial call to determine the string length.
pull/703/head
Ken Raffenetti 4 years ago
parent
commit
c321610c43
1 changed files with 5 additions and 2 deletions
  1. +5
    -2
      vasprintf_compat.h

+ 5
- 2
vasprintf_compat.h View File

@@ -19,24 +19,27 @@ static int vasprintf(char **buf, const char *fmt, va_list ap)
#endif /* !defined(WIN32) */
int chars;
char *b;
va_list ap2;

if (!buf)
{
return -1;
}

va_copy(ap2, ap);
#ifdef WIN32
chars = _vscprintf(fmt, ap) + 1;
chars = _vscprintf(fmt, ap2) + 1;
#else /* !defined(WIN32) */
/* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
* our buffer like on some 64bit sun systems.... but hey, its time to move on
*/
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap) + 1;
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap2) + 1;
if (chars < 0)
{
chars *= -1;
} /* CAW: old glibc versions have this problem */
#endif /* defined(WIN32) */
va_end(ap2);

b = (char *)malloc(sizeof(char) * chars);
if (!b)


Loading…
Cancel
Save