Skip to content

Commit

Permalink
Wnd_GetText が一文字取りこぼす問題に対処
Browse files Browse the repository at this point in the history
  • Loading branch information
kengoide committed Feb 4, 2021
1 parent 10dda8e commit d4a3547
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sakura_core/apiwrap/StdControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ApiWrap{
}

// ウィンドウテキストを取得するのに必要なバッファを確保する
strText.resize( cchRequired );
strText.resize( cchRequired + 1 );

// GetWindowText() はコピーした文字数を返す。
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextw
Expand Down
16 changes: 16 additions & 0 deletions tests/unittests/test-StdControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ TEST(StdControl, Wnd_GetText)
CNativeW tempText;
ASSERT_FALSE(Wnd_GetText(NULL, tempText));
}

// GitHub #1528 の退行防止テストケース。
// 取得する文字列の長さが basic_string::capacity と同じだった場合に一文字取りこぼしていた。
TEST(StdControl, Wnd_GetText2)
{
wchar_t text[] = L"0123456789012345678901234567890123456789";

std::wstring s;
text[s.capacity()] = L'\0';

HINSTANCE hinstance = GetModuleHandleW(nullptr);
HWND hwnd = CreateWindowExW(0, L"STATIC", text, 0, 1, 1, 1, 1, nullptr, nullptr, hinstance, nullptr);
Wnd_GetText(hwnd, s);
DestroyWindow(hwnd);
ASSERT_STREQ(s.c_str(), text);
}

0 comments on commit d4a3547

Please sign in to comment.