From cbc7498b124550326e2a04a098977062fdadd435 Mon Sep 17 00:00:00 2001 From: Raj Date: Thu, 8 Oct 2026 11:41:22 +0530 Subject: [PATCH] fix(libc): format from a copy of the guest va_list on Linux The Linux vprintf, vsprintf, vsnprintf, vsprintf_s, vfprintf, vasprintf and vsscanf handed the guest's va_list to the host function, which advanced the guest's gp_offset, fp_offset and overflow_arg_area. A caller that formats the same list twice, first to size the output and then to write it, got its second call's arguments from past the end. The Windows path reads its arguments from a copy (LibcDetail::FormatArguments) and guest_formatting checks that vsnprintf leaves the list unchanged. Linux now does the same with va_copy. --- core/libs/prx/libc/src/Formatting.cpp | 42 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/core/libs/prx/libc/src/Formatting.cpp b/core/libs/prx/libc/src/Formatting.cpp index c22ae7c4a..02d9e2360 100644 --- a/core/libs/prx/libc/src/Formatting.cpp +++ b/core/libs/prx/libc/src/Formatting.cpp @@ -167,7 +167,10 @@ int APS5_VABI vasprintf_nid_postfix(char** destination, const char* format, VaLi const char* source = text.c_str(); #else char* text = nullptr; - const int count = ::vasprintf(&text, format, *reinterpret_cast(args)); + std::va_list copy; + va_copy(copy, *reinterpret_cast(args)); + const int count = ::vasprintf(&text, format, copy); + va_end(copy); std::unique_ptr owner(text, std::free); if (count < 0) return -1; const char* source = text; @@ -190,7 +193,10 @@ int APS5_VABI vfprintf_nid_postfix(FileStream* stream, const char* format, VaLis const int result = std::fwrite(buffer.data(), 1, static_cast(count), native) == static_cast(count) ? count : -1; #else - const int result = std::vfprintf(native, format, *reinterpret_cast(args)); + std::va_list copy; + va_copy(copy, *reinterpret_cast(args)); + const int result = std::vfprintf(native, format, copy); + va_end(copy); #endif stream->SyncStatus(); return result; @@ -304,7 +310,11 @@ int APS5_VABI vsscanf_nid_postfix(const char* input, const char* format, VaList* #ifdef _WIN32 return LibcDetail::ScanWindows(input, format, args); #else - return std::vsscanf(input, format, *reinterpret_cast(args)); + std::va_list copy; + va_copy(copy, *reinterpret_cast(args)); + const int result = std::vsscanf(input, format, copy); + va_end(copy); + return result; #endif } @@ -352,8 +362,11 @@ int APS5_VABI vprintf_nid_postfix(const char* str, VaList* c) { #ifdef _WIN32 return LibcDetail::PrintWindows(str, c); #else - std::va_list* va = reinterpret_cast(c); - return std::vprintf(str, *va); + std::va_list copy; + va_copy(copy, *reinterpret_cast(c)); + const int result = std::vprintf(str, copy); + va_end(copy); + return result; #endif } @@ -361,7 +374,11 @@ int APS5_VABI vsprintf_nid_postfix(char* str, const char* format, VaList* args) #ifdef _WIN32 return LibcDetail::FormatWindows(str, SIZE_MAX, format, args); #else - return std::vsprintf(str, format, *reinterpret_cast(args)); + std::va_list copy; + va_copy(copy, *reinterpret_cast(args)); + const int result = std::vsprintf(str, format, copy); + va_end(copy); + return result; #endif } @@ -369,8 +386,11 @@ int APS5_VABI vsnprintf_nid_postfix(char* str, size_t size, const char* format, #ifdef _WIN32 return LibcDetail::FormatWindows(str, size, format, c); #else - std::va_list* va = reinterpret_cast(c); - return std::vsnprintf(str, size, format, *va); + std::va_list copy; + va_copy(copy, *reinterpret_cast(c)); + const int result = std::vsnprintf(str, size, format, copy); + va_end(copy); + return result; #endif } @@ -395,7 +415,11 @@ int APS5_VABI printf_s_nid_postfix(const char* format, ...) { #else int APS5_VABI vsprintf_s_nid_postfix(char* buffer, size_t size, const char* format, VaList* args) { - return std::vsnprintf(buffer, size, format, *reinterpret_cast(args)); + std::va_list copy; + va_copy(copy, *reinterpret_cast(args)); + const int result = std::vsnprintf(buffer, size, format, copy); + va_end(copy); + return result; } int APS5_VABI sprintf_s_nid_postfix(char* buffer, size_t size, const char* format, ...) {