From 47c1b8e592a1ec577352675122ad2445a44359fa Mon Sep 17 00:00:00 2001 From: Madan Kumar Date: Wed, 7 Oct 2026 16:25:11 +0530 Subject: [PATCH] fix(libc): only fail sscanf_s fields when the buffer capped the width A %s or %[ field was treated as overflowed whenever the next input character could continue it, even when the field width from the format ended the read. sscanf_s("abcdef", "%3s", buf, 10) returned 0 with an empty buffer instead of 1 with "abc". Run the overflow check only when the buffer capacity reduced the width. --- core/libs/prx/libc/src/Formatting.cpp | 4 +++- core/libs/tests/GuestStrings.cpp | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libc/src/Formatting.cpp b/core/libs/prx/libc/src/Formatting.cpp index 875bdf156..f5d76f15f 100644 --- a/core/libs/prx/libc/src/Formatting.cpp +++ b/core/libs/prx/libc/src/Formatting.cpp @@ -95,6 +95,7 @@ int ScanGuest(const char* buffer, const char* format, bool secure, NextPointer n if (*probe == '\0') return finish(converted ? assigned : EOF); } const bool sized = secure && (conversion == 's' || conversion == 'c' || conversion == '['); + bool capped = false; std::string directive = "%"; void* target = nullptr; if (suppress) directive += '*'; @@ -107,6 +108,7 @@ int ScanGuest(const char* buffer, const char* format, bool secure, NextPointer n if (capacity != 0) static_cast(target)[0] = '\0'; return finish(assigned); } + capped = wanted == 0 || wanted > limit; width = wanted == 0 ? limit : std::min(wanted, limit); } if (width != 0) directive += std::to_string(width); @@ -116,7 +118,7 @@ int ScanGuest(const char* buffer, const char* format, bool secure, NextPointer n int consumed = -1; const int matched = suppress ? std::sscanf(input, directive.c_str(), &consumed) : std::sscanf(input, directive.c_str(), target, &consumed); if (consumed < 0 || (!suppress && matched != 1)) return finish(!converted && *input == '\0' ? EOF : assigned); - if (sized && !suppress && conversion != 'c') { + if (capped && conversion != 'c') { const char next = input[consumed]; bool overflow = false; if (conversion == 's') overflow = next != '\0' && !std::isspace(static_cast(next)); diff --git a/core/libs/tests/GuestStrings.cpp b/core/libs/tests/GuestStrings.cpp index 145980fed..019e4425c 100644 --- a/core/libs/tests/GuestStrings.cpp +++ b/core/libs/tests/GuestStrings.cpp @@ -93,6 +93,11 @@ static void CheckSscanfS() { Require(sscanf_s_nid_postfix(" 12 abc x", "%d %s %c", &number, word, 4u, &letter, 1u) == 3 && number == 12 && std::strcmp(word, "abc") == 0 && letter == 'x'); Require(sscanf_s_nid_postfix("12 abcd", "%d %s", &number, word, 4u) == 1 && word[0] == '\0'); Require(sscanf_s_nid_postfix("key=val", "%3[a-z]=%3s", word, 4u, value, 8u) == 2 && std::strcmp(word, "key") == 0 && std::strcmp(value, "val") == 0); + Require(sscanf_s_nid_postfix("abcdef", "%3s", word, 4u) == 1 && std::strcmp(word, "abc") == 0); + Require(sscanf_s_nid_postfix("abcdef", "%3s", value, 8u) == 1 && std::strcmp(value, "abc") == 0); + Require(sscanf_s_nid_postfix("abcdef", "%3[a-z]", value, 8u) == 1 && std::strcmp(value, "abc") == 0); + Require(sscanf_s_nid_postfix("2024ABCD 7", "%4s%s", word, 4u, value, 8u) == 0 && word[0] == '\0'); + Require(sscanf_s_nid_postfix("2024ABCD", "%3s%4s", word, 4u, value, 8u) == 2 && std::strcmp(word, "202") == 0 && std::strcmp(value, "4ABC") == 0); int position = 0; Require(sscanf_s_nid_postfix("7 %", "%d %%%n", &number, &position) == 1 && position == 3); Require(sscanf_s_nid_postfix(" ", "%d", &number) == EOF);