From f1ddc19806867020b3ce04f31c57ee1341ed7974 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 2 Nov 2023 14:12:52 +0100 Subject: [PATCH] [winpr,str] add winpr_vasprintf --- winpr/include/winpr/string.h | 1 + winpr/libwinpr/crt/string.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/winpr/include/winpr/string.h b/winpr/include/winpr/string.h index 62edc68b7..26114412c 100644 --- a/winpr/include/winpr/string.h +++ b/winpr/include/winpr/string.h @@ -40,6 +40,7 @@ extern "C" const char* separator); WINPR_API int winpr_asprintf(char** s, size_t* slen, const char* templ, ...); + WINPR_API int winpr_vasprintf(char** s, size_t* slen, const char* templ, va_list ap); #ifndef _WIN32 diff --git a/winpr/libwinpr/crt/string.c b/winpr/libwinpr/crt/string.c index 7ff7fcb1d..af3502a21 100644 --- a/winpr/libwinpr/crt/string.c +++ b/winpr/libwinpr/crt/string.c @@ -180,12 +180,21 @@ int winpr_asprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, . { va_list ap; - WINPR_ASSERT(s); - WINPR_ASSERT(slen); + va_start(ap, templ); + int rc = winpr_vasprintf(s, slen, templ, ap); + va_end(ap); + return rc; +} + +WINPR_ATTR_FORMAT_ARG(3, 0) +int winpr_vasprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, va_list oap) +{ + va_list ap; + *s = NULL; *slen = 0; - va_start(ap, templ); + va_copy(ap, oap); const int length = vsnprintf(NULL, 0, templ, ap); va_end(ap); if (length < 0) @@ -195,7 +204,7 @@ int winpr_asprintf(char** s, size_t* slen, WINPR_FORMAT_ARG const char* templ, . if (!str) return -1; - va_start(ap, templ); + va_copy(ap, oap); const int plen = vsprintf(str, templ, ap); va_end(ap);