Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
9d1e14b
fix(libc): retain short wide scanf character fields
UnhealthyKraken Oct 22, 2026
26db0f2
docs(libc): keep short-field evidence limits concise
UnhealthyKraken Oct 22, 2026
36496b7
chore(libc): refresh short-field scanf against current main
UnhealthyKraken Oct 22, 2026
a97e5e4
Merge remote-tracking branch 'origin/main' into fix/libc-wide-scan-sh…
UnhealthyKraken Oct 22, 2026
c677508
chore: refresh wide scanner review against current main
UnhealthyKraken Oct 23, 2026
e55cd07
Merge remote-tracking branch 'origin/main' into fix/libc-wide-scan-sh…
UnhealthyKraken Oct 23, 2026
c3eb4c2
test(libc): cover positive secure wide character capacities
cookie42073-cmd Oct 23, 2026
1244656
fix(libc): bound secure wide character fields by positive capacity
cookie42073-cmd Oct 23, 2026
ad9fac6
Merge remote-tracking branch 'origin/main' into fix/libc-wide-scan-sh…
UnhealthyKraken Oct 23, 2026
664b4af
test(libc): cover zero-capacity wide character results
UnhealthyKraken Oct 23, 2026
925d839
fix(libc): return no assignment for zero-capacity wide fields
UnhealthyKraken Oct 23, 2026
5f290ce
Merge remote-tracking branch 'origin/main' into fix/libc-wide-scan-sh…
UnhealthyKraken Oct 25, 2026
b48707c
chore(libc): merge main and reconcile scanner debt
UnhealthyKraken Oct 27, 2026
1501528
Merge main into fix/libc-wide-scan-short-field
oneandonlydean Nov 1, 2026
850791e
Merge main 88e804b0a7 into #9863 (1501528f73)
oneandonlydean Nov 2, 2026
a4b6083
Merge commit '4f64f81ef7b4bca975b4b9f3dfd15dfff56f716f' into HEAD
oneandonlydean Nov 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/libs/prx/libc/src/ScanningWide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,15 @@ class WideScanner {
if (input.Peek() < 0) return Outcome::Input;
std::u16string item;
if (directive.conversion == u'c') {
const std::size_t count = directive.width == 0 ? 1 : directive.width;
std::size_t count = directive.width == 0 ? 1 : directive.width;
if (sized && target != nullptr && directive.length == "l" && capacity != 0 && capacity < count) count = capacity;
while (item.size() < count && input.Peek() >= 0) {
item.push_back(static_cast<char16_t>(input.Peek()));
input.Advance();
}
if (item.size() < count) return Outcome::Matching;
if (sized && target != nullptr && directive.length == "l" && capacity == 0) return Outcome::Matching;
if (item.size() < count && sized && target != nullptr && capacity < count)
Fail("short character field with capacity below width");
} else {
const std::size_t limit = directive.width == 0 ? std::numeric_limits<std::size_t>::max() : directive.width;
while (item.size() < limit && input.Peek() >= 0) {
Expand Down
105 changes: 102 additions & 3 deletions core/libs/tests/GuestWideConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ static void CheckScan() {
Require(swscanf_s_nid_postfix(u"abc def", u"%s", narrow, std::size_t{4}) == 1 && std::strcmp(narrow, "abc") == 0, "%s");
char16_t pair[3] = {u'?', u'?', u'?'};
Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{2}) == 1 && pair[0] == u'x' && pair[1] == u'y' && pair[2] == u'?', "%2lc");
Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{1}) == 0, "%2lc too small");
Require(swscanf_s_nid_postfix(u"x", u"%2lc", pair, std::size_t{2}) == 0, "%2lc short input");
Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{1}) == 1 && pair[0] == u'x', "%2lc limited by capacity");
Require(swscanf_s_nid_postfix(u"x", u"%2lc", pair, std::size_t{2}) == 1 && pair[0] == u'x', "%2lc short input");
Require(swscanf_s_nid_postfix(u"cab12", u"%l[abc]%d", text, std::size_t{6}, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "%l[");
Require(swscanf_s_nid_postfix(u"été!", u"%l[^!]", text, std::size_t{6}) == 1 && std::u16string(text) == u"été", "%l[^");
char16_t untouched[4] = {u'?', u'?', u'?', u'?'};
Expand Down Expand Up @@ -227,7 +227,7 @@ static void CheckUnsizedScan() {
Require(swscanf_nid_postfix(u"abc def", u"%s%d", narrow, &first) == 1 && std::strcmp(narrow, "abc") == 0, "swscanf %s");
char16_t pair[3] = {u'?', u'?', u'?'};
Require(swscanf_nid_postfix(u"xyz", u"%2lc", pair) == 1 && pair[0] == u'x' && pair[1] == u'y' && pair[2] == u'?', "swscanf %2lc");
Require(swscanf_nid_postfix(u"x", u"%2lc", pair) == 0, "swscanf %2lc short input");
Require(swscanf_nid_postfix(u"x", u"%2lc", pair) == 1 && pair[0] == u'x', "swscanf %2lc short input");
char letter = 0;
Require(swscanf_nid_postfix(u" q7", u"%c%d", &letter, &first) == 1 && letter == ' ', "swscanf %c does not skip white space");
Require(swscanf_nid_postfix(u"cab12", u"%l[abc]%d", text, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "swscanf %l[");
Expand Down Expand Up @@ -296,7 +296,106 @@ static void CheckUnsizedScan() {
Require(swscanf_nid_postfix(u"Ā", u"%s", narrow) == 1 && std::strcmp(narrow, "\xc4\x80") == 0, "swscanf narrow %s stores UTF-8");
}

static void CheckShortCharacterFields() {
const char16_t* inputs[]{u"", u"a", u"ab", u"abc", u"abcd"};
for (const char16_t* input : inputs) {
const std::size_t length = std::u16string(input).size();
const std::size_t consumed = length < 3 ? length : 3;
for (bool sized : {false, true}) {
char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a};
int count = -1;
const int result = sized ? swscanf_s_nid_postfix(input, u"%3lc%n", output + 1, 3u, &count)
: swscanf_nid_postfix(input, u"%3lc%n", output + 1, &count);
Require(result == (length == 0 ? EOF : 1), "short wide character field assignment count");
Require(count == (length == 0 ? -1 : static_cast<int>(consumed)), "short wide character field consumed count");
for (std::size_t index = 0; index < 5; ++index) {
const char16_t expected = index > 0 && index <= consumed ? input[index - 1] : 0xa55a;
Require(output[index] == expected, "short wide character field exact write extent without terminator");
}
count = -1;
const int suppressed = sized ? swscanf_s_nid_postfix(input, u"%*3lc%n", &count)
: swscanf_nid_postfix(input, u"%*3lc%n", &count);
Require(suppressed == (length == 0 ? EOF : 0), "suppressed short character field assignment count");
Require(count == (length == 0 ? -1 : static_cast<int>(consumed)), "suppressed short character field continues to count");
}
}
char16_t output[]{0xa55a, 0xa55a, 0xa55a};
int value = -1, count = -1;
Require(swscanf_nid_postfix(u"5 a", u"%d %3lc%n", &value, output, &count) == 2 && value == 5 && count == 3,
"short character field after another assignment");
Require(output[0] == u'a' && output[1] == 0xa55a && output[2] == 0xa55a, "short field after assignment does not terminate");
char bytes[]{'?', '?', '?', '?'};
count = -1;
Require(swscanf_nid_postfix(u"ab", u"%3c%n", bytes + 1, &count) == 1 && count == 2,
"short narrow destination follows FreeBSD character conversion");
Require(bytes[0] == '?' && bytes[1] == 'a' && bytes[2] == 'b' && bytes[3] == '?', "short narrow destination bounds");
}

static void CheckZeroCapacityCharacterFields() {
for (const char16_t* input : {u"a", u"ab", u"abc", u"abcd"}) {
char16_t output[]{0xa55a, 0xa55a, 0xa55a};
int count = -1, result = -1;
*__error_nid_postfix() = 0;
Require(!Throws([&] { result = swscanf_s_nid_postfix(input, u"%3lc%n", output + 1, 0u, &count); }),
"zero-capacity wide character field does not throw");
const int error = *__error_nid_postfix();
Require(result == 0, "zero-capacity wide character field returns no assignment");
Require(count == -1, "zero-capacity wide character field does not reach percent n");
Require(error == 0, "zero-capacity wide character field preserves errno");
for (const char16_t unit : output) Require(unit == 0xa55a, "zero-capacity wide character field leaves output untouched");
}
char16_t output[]{0xa55a, 0xa55a, 0xa55a};
int value = -1, count = -1, result = -1;
Require(!Throws([&] { result = swscanf_s_nid_postfix(u"7 a", u"%d %3lc%n", &value, output + 1, 0u, &count); }),
"zero-capacity field after an assignment does not throw");
Require(result == 1 && value == 7 && count == -1, "zero-capacity field retains only prior assignments");
for (const char16_t unit : output) Require(unit == 0xa55a, "zero-capacity field after an assignment leaves output untouched");
}

static void CheckSecureCharacterCapacities() {
const char16_t* inputs[]{u"", u"a", u"ab", u"abc", u"abcd"};
for (const char16_t* input : inputs) {
const std::size_t length = std::u16string(input).size();
for (unsigned int capacity : {1u, 2u, 3u, 4u}) {
const std::size_t limit = capacity < 3 ? capacity : 3;
const std::size_t consumed = length < limit ? length : limit;
char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a};
char16_t next = 0xa55a;
int count = -1;
int result = 0;
*__error_nid_postfix() = 0;
if (Throws([&] { result = swscanf_s_nid_postfix(input, u"%3lc%n%lc", output + 1, capacity, &count, &next, 1u); })) {
Require(false, "positive secure character capacity does not throw");
continue;
}
const int expected = length == 0 ? EOF : length > consumed ? 2 : 1;
Require(result == expected, "positive secure character capacity assignment count");
Require(count == (length == 0 ? -1 : static_cast<int>(consumed)), "positive secure character capacity consumed count");
Require(next == (length > consumed ? input[consumed] : 0xa55a), "the next conversion sees the capacity-limited remainder");
Require(*__error_nid_postfix() == 0, "positive secure character capacity preserves errno");
for (std::size_t index = 0; index < 6; ++index) {
const char16_t expectedUnit = index > 0 && index <= consumed ? input[index - 1] : 0xa55a;
Require(output[index] == expectedUnit, "positive secure character capacity exact writes without terminator");
}
}
}
char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a};
char16_t next = 0xa55a;
int value = -1, count = -1;
Require(swscanf_s_nid_postfix(u"7 abcZ", u"%d %3lc%n%lc", &value, output + 1, 2u, &count, &next, 1u) == 3
&& value == 7 && count == 4 && next == u'c', "positive secure character capacity after another assignment");
Require(output[0] == 0xa55a && output[1] == u'a' && output[2] == u'b' && output[3] == 0xa55a,
"positive secure character capacity after another assignment preserves bounds");
count = -1;
next = 0xa55a;
Require(swscanf_s_nid_postfix(u"abcd", u"%*3lc%n%lc", &count, &next, 1u) == 1 && count == 3 && next == u'd',
"suppressed character fields keep width and consume no capacity argument");
}

int main() {
CheckZeroCapacityCharacterFields();
CheckSecureCharacterCapacities();
CheckShortCharacterFields();
CheckStoul();
CheckDinkumwareConversions();
CheckClassification();
Expand Down
94 changes: 94 additions & 0 deletions core/libs/tests/GuestWideStreamScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,97 @@ static void CheckStandardInput() {
Close(stream);
}

static void CheckShortCharacterFields() {
const char* inputs[]{"", "a", "ab", "abc", "abcd"};
for (const char* input : inputs) {
const std::size_t length = std::string(input).size();
const std::size_t consumed = length < 3 ? length : 3;
for (int mode = 0; mode < 4; ++mode) {
FileStream* stream = Open(input);
char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a};
int count = -1;
const int result = mode == 0 ? fwscanf_nid_postfix(stream, u"%3lc%n", output + 1, &count)
: mode == 1 ? ScanStream(stream, u"%3lc%n", output + 1, &count)
: mode == 2 ? fwscanf_s_nid_postfix(stream, u"%3lc%n", output + 1, 3u, &count)
: ScanStreamSized(stream, u"%3lc%n", output + 1, 3u, &count);
Require(result == (length == 0 ? EOF : 1), "short stream character assignment count");
Require(count == (length == 0 ? -1 : static_cast<int>(consumed)), "short stream character consumed count");
for (std::size_t index = 0; index < 5; ++index) {
const char16_t expected = index > 0 && index <= consumed ? static_cast<char16_t>(input[index - 1]) : 0xa55a;
Require(output[index] == expected, "short stream character exact write extent without terminator");
}
Require(fgetwc_nid_postfix(stream) == (length > 3 ? 'd' : -1), "short stream character lookahead");
Close(stream);
}
}
for (bool sized : {false, true}) {
FileStream* stream = Open("ab");
int count = -1;
const int result = sized ? ScanStreamSized(stream, u"%*3lc%n", &count) : ScanStream(stream, u"%*3lc%n", &count);
Require(result == 0 && count == 2, "suppressed short stream field continues to count");
Require(fgetwc_nid_postfix(stream) == -1, "suppressed short stream field consumes input");
Close(stream);
}
}

static void CheckZeroCapacityCharacterFields() {
for (bool indirect : {false, true}) {
FileStream* stream = Open("a");
char16_t output[]{0xa55a, 0xa55a, 0xa55a};
int count = -1;
*__error_nid_postfix() = 0;
const int result = indirect ? ScanStreamSized(stream, u"%3lc%n", output + 1, 0u, &count)
: fwscanf_s_nid_postfix(stream, u"%3lc%n", output + 1, 0u, &count);
const int error = *__error_nid_postfix();
Require(result == 0, "zero-capacity stream character field returns no assignment");
Require(count == -1, "zero-capacity stream character field does not reach percent n");
Require(error == 0, "zero-capacity stream character field preserves errno");
for (const char16_t unit : output) Require(unit == 0xa55a, "zero-capacity stream character field leaves output untouched");
Close(stream);
}
}

static void CheckSecureCharacterCapacities() {
const char* inputs[]{"", "a", "ab", "abc", "abcd"};
for (const char* input : inputs) {
const std::size_t length = std::string(input).size();
for (unsigned int capacity : {1u, 2u, 3u, 4u}) {
const std::size_t limit = capacity < 3 ? capacity : 3;
const std::size_t consumed = length < limit ? length : limit;
for (bool indirect : {false, true}) {
FileStream* stream = Open(input);
char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a};
int count = -1;
*__error_nid_postfix() = 0;
const int result = indirect ? ScanStreamSized(stream, u"%3lc%n", output + 1, capacity, &count)
: fwscanf_s_nid_postfix(stream, u"%3lc%n", output + 1, capacity, &count);
Require(result == (length == 0 ? EOF : 1), "positive secure stream character capacity assignment count");
Require(count == (length == 0 ? -1 : static_cast<int>(consumed)), "positive secure stream character capacity consumed count");
Require(*__error_nid_postfix() == 0, "positive secure stream character capacity preserves errno");
for (std::size_t index = 0; index < 6; ++index) {
const char16_t expectedUnit = index > 0 && index <= consumed ? static_cast<char16_t>(input[index - 1]) : 0xa55a;
Require(output[index] == expectedUnit, "positive secure stream character capacity exact writes without terminator");
}
Require(fgetwc_nid_postfix(stream) == (length > consumed ? input[consumed] : -1),
"positive secure stream character capacity leaves lookahead");
Close(stream);
}
}
}
for (bool indirect : {false, true}) {
FileStream* stream = Open("7 abcZ");
char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a};
int value = -1, count = -1;
const int result = indirect ? ScanStreamSized(stream, u"%d %3lc%n", &value, output + 1, 2u, &count)
: fwscanf_s_nid_postfix(stream, u"%d %3lc%n", &value, output + 1, 2u, &count);
Require(result == 2 && value == 7 && count == 4, "positive secure stream capacity after another assignment");
Require(output[0] == 0xa55a && output[1] == u'a' && output[2] == u'b' && output[3] == 0xa55a,
"positive secure stream capacity after another assignment preserves bounds");
Require(fgetwc_nid_postfix(stream) == u'c', "positive secure stream capacity after another assignment leaves lookahead");
Close(stream);
}
}

static void CheckNarrowWideFailurePosition() {
struct Case {
const char* input;
Expand Down Expand Up @@ -303,6 +394,9 @@ static void CheckNarrowWideFailurePosition() {
}

int main() {
CheckZeroCapacityCharacterFields();
CheckSecureCharacterCapacities();
CheckShortCharacterFields();
CheckNarrowWideFailurePosition();
CheckConsumption();
CheckWideText();
Expand Down
Loading
Loading