From 992933b95c593543b39516c8d0ddec265450e081 Mon Sep 17 00:00:00 2001 From: DotDebian Date: Mon, 5 Oct 2026 09:59:30 +0200 Subject: [PATCH] feat(libc): implement snwprintf_s Format through the shared wide formatter, truncate to the buffer and return the full length, as C11 K.3.9.1.3 specifies. A null buffer or format, a zero or oversized n, %n and a null %s argument are runtime-constraint violations that return a negative value. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libc/src/FormattingWide.cpp | 26 ++++++++++- core/libs/prx/libc/src/RuntimeSupport.cpp | 5 --- core/libs/tests/GuestWideFormatting.cpp | 55 +++++++++++++++++++++++ docs/dev/TechnicalDebt.md | 2 +- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/core/libs/prx/libc/src/FormattingWide.cpp b/core/libs/prx/libc/src/FormattingWide.cpp index 6684ffc68..ad53b9862 100644 --- a/core/libs/prx/libc/src/FormattingWide.cpp +++ b/core/libs/prx/libc/src/FormattingWide.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -88,7 +89,7 @@ void AppendPadded(std::u16string& out, const std::u16string& text, bool left, in if (left) out.append(pad, u' '); } -std::u16string FormatWide(const char16_t* format, VaList* source) { +std::u16string FormatWide(const char16_t* format, VaList* source, bool rejectNullStrings = false) { if (format == nullptr || source == nullptr) throw std::invalid_argument("Null formatting argument"); LibcDetail::FormatArguments args(source); std::u16string out; @@ -171,10 +172,12 @@ std::u16string FormatWide(const char16_t* format, VaList* source) { const std::size_t limit = precision < 0 ? SIZE_MAX : static_cast(precision); if (conversion == u'S' || length == "l") { const char16_t* value = args.Next(); + if (value == nullptr && rejectNullStrings) throw std::invalid_argument("Null string argument"); if (value == nullptr) value = u"(null)"; for (std::size_t index = 0; index < limit && value[index] != 0; ++index) text.push_back(value[index]); } else { const char* value = args.Next(); + if (value == nullptr && rejectNullStrings) throw std::invalid_argument("Null string argument"); AppendUtf16(text, value != nullptr ? value : "(null)", limit); } AppendPadded(out, text, left, width); @@ -204,6 +207,27 @@ int APS5_VABI vswprintf_nid_postfix(char16_t* buffer, std::size_t size, const ch } } +int APS5_VABI snwprintf_s_nid_postfix(char16_t* buffer, std::size_t size, const char16_t* format, ...) { + constexpr std::size_t RsizeMax = SIZE_MAX >> 1; + int result = -1; + if (buffer != nullptr && format != nullptr && size != 0 && size <= RsizeMax) { + APS5_VA_BEGIN(format); + try { + const std::u16string text = FormatWide(format, reinterpret_cast(args), true); + if (text.size() <= static_cast(INT_MAX)) { + const std::size_t copied = text.size() < size - 1 ? text.size() : size - 1; + std::memcpy(buffer, text.data(), copied * sizeof(char16_t)); + buffer[copied] = 0; + result = static_cast(text.size()); + } + } catch (const std::invalid_argument&) { + } + APS5_VA_END(); + } + if (result < 0 && buffer != nullptr && size != 0 && size < RsizeMax) buffer[0] = 0; + return result; +} + int APS5_VABI wprintf_nid_postfix(const char16_t* format, ...) { APS5_VA_BEGIN(format); int result = -1; diff --git a/core/libs/prx/libc/src/RuntimeSupport.cpp b/core/libs/prx/libc/src/RuntimeSupport.cpp index 3fe661094..ef00dd455 100644 --- a/core/libs/prx/libc/src/RuntimeSupport.cpp +++ b/core/libs/prx/libc/src/RuntimeSupport.cpp @@ -138,9 +138,4 @@ void APS5_VABI _Unlocksyslock_nid_postfix() { g_sysLock.unlock(); } -int APS5_VABI snwprintf_s() { - NotImplemented_nid_no_patch(__func__); - return 0; -} - } diff --git a/core/libs/tests/GuestWideFormatting.cpp b/core/libs/tests/GuestWideFormatting.cpp index 2aea154dd..343ef0e51 100644 --- a/core/libs/tests/GuestWideFormatting.cpp +++ b/core/libs/tests/GuestWideFormatting.cpp @@ -1,11 +1,13 @@ #include "SceTypes.hpp" #include "prx/libc/include/general/VabiMacros.hpp" #include +#include #include #include #include extern "C" int APS5_VABI vswprintf_nid_postfix(char16_t*, std::size_t, const char16_t*, VaList*); +extern "C" int APS5_VABI snwprintf_s_nid_postfix(char16_t*, std::size_t, const char16_t*, ...); static int APS5_VABI Format(char16_t* buffer, std::size_t size, const char16_t* format, ...) { #ifdef _WIN32 @@ -34,7 +36,60 @@ static void Check(const char16_t* format, const char* input, const char16_t* exp } } +static void Require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "snwprintf_s: %s\n", message); + std::abort(); + } +} + +static void CheckBounded() { + constexpr std::size_t rsizeMax = SIZE_MAX >> 1; + const char16_t* const nullWide = nullptr; + const char* const nullNarrow = nullptr; + char16_t buffer[16]; + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 16, u"%d-%ls-%s", 42, u"ab", "\xc3\xa9") == 7, "complete output length"); + Require(buffer == std::u16string(u"42-ab-\u00e9"), "complete output"); + + buffer[5] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 5, u"%d-%ls", 42, u"abcd") == 7, "truncated output length"); + Require(buffer == std::u16string(u"42-a") && buffer[5] == u'x', "truncated output"); + + buffer[0] = u'x'; + buffer[1] = u'y'; + Require(snwprintf_s_nid_postfix(buffer, 1, u"%x", 0xabcu) == 3, "one unit buffer length"); + Require(buffer[0] == 0 && buffer[1] == u'y', "one unit buffer"); + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 16, u"") == 0 && buffer[0] == 0, "empty format"); + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 16, nullptr) < 0 && buffer[0] == 0, "null format"); + Require(snwprintf_s_nid_postfix(nullptr, 16, u"a") < 0, "null buffer"); + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 0, u"a") < 0 && buffer[0] == u'x', "zero size"); + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, rsizeMax + 1, u"a") < 0 && buffer[0] == u'x', "size above RSIZE_MAX"); + + int written = -7; + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 16, u"ab%n", &written) < 0 && buffer[0] == 0 && written == -7, "%n"); + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 16, u"a%ls", nullWide) < 0 && buffer[0] == 0, "null %ls argument"); + + buffer[0] = u'x'; + Require(snwprintf_s_nid_postfix(buffer, 16, u"a%s", nullNarrow) < 0 && buffer[0] == 0, "null %s argument"); + + Require(Format(buffer, 16, u"%s", nullNarrow) == 6 && buffer == std::u16string(u"(null)"), "vswprintf null %s argument"); +} + int main() { + CheckBounded(); Check(u"%.2s", "\xc3\xa9\xc3\xa8", u"\u00e9\u00e8"); Check(u"%.1s", "\xc3\xa9\xc3\xa8", u"\u00e9"); Check(u"%.0s", "\xc3\xa9", u""); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index c7520ce93..4c6ab2bef 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -70,7 +70,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceSystemServiceDisableMediaPlay](../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - unknown signature - [sceSystemServiceReenableMediaPlay](../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - unknown signature - [sceVoiceSetMuteFlag](../../core/libs/prx/libSceVoice/Export.cpp) (libSceVoice) - unknown signature -- [snwprintf_s](../../core/libs/prx/libc/src/RuntimeSupport.cpp) (libc) - unknown signature +- [snwprintf_s](../../core/libs/prx/libc/src/FormattingWide.cpp) (libc) - follows C11 K.3.9.1.3 with `RSIZE_MAX` assumed to be `SIZE_MAX >> 1`. A runtime-constraint violation only returns a negative value: `set_constraint_handler_s` is not exported and the console's default handler is unknown. An invalid multibyte `%s` argument is copied byte by byte instead of being reported as an encoding error, as in `vswprintf` - [libSceUlt queues](../../core/libs/prx/libSceUlt/Export.cpp) - the added `sceUltQueuePop`, `sceUltQueueTryPush`, `sceUltQueueDestroy` and `sceUltQueueDataResourcePoolDestroy` signatures are inferred from the existing push/pop and object APIs. Queue destruction is assumed to reject blocked callers with `ULT_ERROR_BUSY` and otherwise discard queued data; pool exhaustion returns `ULT_ERROR_AGAIN`. These rules, alignment checks and shared slot limits have not been confirmed on PS5 hardware or a title. Non-null queue and data-pool option parameters throw because their layouts are unknown. ### Functional