mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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.
This commit is contained in:
@@ -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<char*>(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<unsigned char>(next));
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user