From c321610c433c00bd8c488fa9c830c1704406dbb0 Mon Sep 17 00:00:00 2001 From: Ken Raffenetti Date: Mon, 3 May 2021 14:13:03 -0500 Subject: [PATCH] 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. --- vasprintf_compat.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/vasprintf_compat.h b/vasprintf_compat.h index 5264272..c3fe6f0 100644 --- a/vasprintf_compat.h +++ b/vasprintf_compat.h @@ -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)