From d4a3547c998a56db4172b9406f6aa09fa13707d0 Mon Sep 17 00:00:00 2001 From: Kengo Ide Date: Thu, 4 Feb 2021 18:08:59 +0900 Subject: [PATCH] =?UTF-8?q?Wnd=5FGetText=20=E3=81=8C=E4=B8=80=E6=96=87?= =?UTF-8?q?=E5=AD=97=E5=8F=96=E3=82=8A=E3=81=93=E3=81=BC=E3=81=99=E5=95=8F?= =?UTF-8?q?=E9=A1=8C=E3=81=AB=E5=AF=BE=E5=87=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/apiwrap/StdControl.cpp | 2 +- tests/unittests/test-StdControl.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sakura_core/apiwrap/StdControl.cpp b/sakura_core/apiwrap/StdControl.cpp index 79701c8ace..855af35c33 100644 --- a/sakura_core/apiwrap/StdControl.cpp +++ b/sakura_core/apiwrap/StdControl.cpp @@ -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 diff --git a/tests/unittests/test-StdControl.cpp b/tests/unittests/test-StdControl.cpp index 1009d1bab3..bd5bff128c 100644 --- a/tests/unittests/test-StdControl.cpp +++ b/tests/unittests/test-StdControl.cpp @@ -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); +}