Index: libcxx/include/__support/ibm/xlocale.h =================================================================== --- libcxx/include/__support/ibm/xlocale.h +++ libcxx/include/__support/ibm/xlocale.h @@ -10,6 +10,7 @@ #ifndef _LIBCPP_SUPPORT_IBM_XLOCALE_H #define _LIBCPP_SUPPORT_IBM_XLOCALE_H +#include #include <__support/ibm/locale_mgmt_aix.h> #include "cstdlib" @@ -251,18 +252,19 @@ } static inline -int vasprintf(char **strp, const char *fmt, va_list ap) -{ +int vasprintf(char **strp, const char *fmt, va_list ap) { const size_t buff_size = 256; - int str_size; - if ((*strp = (char *)malloc(buff_size)) == NULL) - { + if ((*strp = (char *)malloc(buff_size)) == NULL) { return -1; } - if ((str_size = vsnprintf(*strp, buff_size, fmt, ap)) >= buff_size) - { - if ((*strp = (char *)realloc(*strp, str_size + 1)) == NULL) - { + + va_list ap_copy; + va_copy(ap_copy, ap); + int str_size = vsnprintf(*strp, buff_size, fmt, ap_copy); + va_end(ap_copy); + + if ((size_t) str_size >= buff_size) { + if ((*strp = (char *)realloc(*strp, str_size + 1)) == NULL) { return -1; } str_size = vsnprintf(*strp, str_size + 1, fmt, ap); Index: libcxx/src/support/win32/locale_win32.cpp =================================================================== --- libcxx/src/support/win32/locale_win32.cpp +++ libcxx/src/support/win32/locale_win32.cpp @@ -115,7 +115,11 @@ int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap ) { __libcpp_locale_guard __current(loc); - return __libcpp_vasprintf( ret, format, ap ); + va_list ap_copy; + va_copy(ap_copy, ap); + int str_size = __libcpp_vasprintf( ret, format, ap_copy ); + va_end(ap_copy); + return str_size; } #if !defined(_LIBCPP_MSVCRT)