Skip to content

Commit

Permalink
Fix uninitialized memory access in GetConsoleTitleA test (microsoft#1…
Browse files Browse the repository at this point in the history
…2699)

`WideCharToMultiByte` doesn't write a final null-byte by default.
`til::u16u8` avoids the problem.

## PR Checklist
* [x] I work here
* [x] Tests added/passed

## Validation Steps Performed
* Test passes in Debug builds ✅

(cherry picked from commit 5072ee6)
  • Loading branch information
lhecker authored and DHowett committed Mar 23, 2022
1 parent 9c1fe1f commit 442a46e
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions src/host/ut_host/ApiRoutinesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,10 @@ class ApiRoutinesTests
TEST_METHOD(ApiGetConsoleTitleA)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
gci.SetTitle(L"Test window title.");

const auto title = gci.GetTitle();

int const iBytesNeeded = WideCharToMultiByte(gci.OutputCP,
0,
title.data(),
gsl::narrow_cast<int>(title.size()),
nullptr,
0,
nullptr,
nullptr);

wistd::unique_ptr<char[]> pszExpected = wil::make_unique_nothrow<char[]>(iBytesNeeded);
VERIFY_IS_NOT_NULL(pszExpected);

VERIFY_WIN32_BOOL_SUCCEEDED(WideCharToMultiByte(gci.OutputCP,
0,
title.data(),
gsl::narrow_cast<int>(title.size()),
pszExpected.get(),
iBytesNeeded,
nullptr,
nullptr));
// SetTitle() runs some extra code. Let's not skip it since this is a test.
gci.SetTitle(L"Test window title.");
const auto pszExpected = til::u16u8(gci.GetTitle());

char pszTitle[MAX_PATH]; // most applications use MAX_PATH
size_t cchWritten = 0;
Expand All @@ -249,7 +229,7 @@ class ApiRoutinesTests
// NOTE: W version of API returns string length. A version of API returns buffer length (string + null).
VERIFY_ARE_EQUAL(gci.GetTitle().length() + 1, cchWritten);
VERIFY_ARE_EQUAL(gci.GetTitle().length(), cchNeeded);
VERIFY_ARE_EQUAL(WEX::Common::String(pszExpected.get()), WEX::Common::String(pszTitle));
VERIFY_ARE_EQUAL(std::string_view{ pszExpected }, std::string_view{ pszTitle });
}

TEST_METHOD(ApiGetConsoleTitleW)
Expand Down

0 comments on commit 442a46e

Please sign in to comment.