Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a ReadConsoleOutputCharacter regression #16898

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
17 changes: 17 additions & 0 deletions src/host/ft_host/CJK_DbcsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@
BEGIN_TEST_METHOD(TestInvalidTrailer)
TEST_METHOD_PROPERTY(L"IsolationLevel", L"Method")
END_TEST_METHOD()

TEST_METHOD(TestNarrowSurrogate);
};

bool DbcsTests::DbcsTestSetup()
Expand Down Expand Up @@ -2183,3 +2185,18 @@

DbcsWriteRead::Verify(expected, output);
}

// The various console APIs that read back from the buffer are generally incompatible with UTF16 and surrogate pairs.
// ReadConsoleOutputCharacterW in particular has a nLength parameter which is a column count but also the buffer size.
// This makes it impossible to reliably return arbitrarily long graphemes per-cell in the output buffer.
// The test ensures that we replace them with U+FFFD which makes the behavior more consistent for the caller.
void DbcsTests::TestNarrowSurrogate()
{
const auto out = GetStdHandle(STD_OUTPUT_HANDLE);
wchar_t buf[3];
DWORD read;

VERIFY_WIN32_BOOL_SUCCEEDED(WriteConsoleOutputCharacterW(out, L"a\U00010000b", 4, {}, &read));
VERIFY_WIN32_BOOL_SUCCEEDED(ReadConsoleOutputCharacterW(out, &buf[0], ARRAYSIZE(buf), {}, &read));
VERIFY_ARE_EQUAL(std::wstring_view(L"a\U0000FFFDb"), std::wstring_view(&buf[0], read));
Fixed Show fixed Hide fixed
}
7 changes: 6 additions & 1 deletion src/host/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ std::wstring ReadOutputStringW(const SCREEN_INFORMATION& screenInfo,
// Otherwise, add anything that isn't a trailing cell. (Trailings are duplicate copies of the leading.)
if (it->DbcsAttr() != DbcsAttribute::Trailing)
{
retVal += it->Chars();
auto chars = it->Chars();
if (chars.size() > 1)
{
chars = { &UNICODE_REPLACEMENT, 1 };
}
retVal += chars;
}
}

Expand Down
Loading