Skip to content

Commit

Permalink
Merge ad4b0cd into 10dda8e
Browse files Browse the repository at this point in the history
  • Loading branch information
kengoide authored Feb 3, 2021
2 parents 10dda8e + ad4b0cd commit fd8966f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sakura_core/apiwrap/StdControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace ApiWrap{

// GetWindowText() はコピーした文字数を返す。
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextw
const int actualCopied = ::GetWindowText( hWnd, strText.data(), (int)strText.capacity() );
const int actualCopied = ::GetWindowText( hWnd, strText.data(), (int)(strText.capacity() + 1) );
if( actualCopied < 0 ){
// 仕様上は負の場合はありえないが、念の為エラーチェックしておく。
return false;
Expand All @@ -51,7 +51,7 @@ namespace ApiWrap{
return false;
}
}
else if( (int)strText.capacity() <= actualCopied ){
else if( (int)strText.capacity() < actualCopied ){
// GetWindowText() の仕様上はありえないはず
return false;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/unittests/test-StdControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
#include <gtest/gtest.h>
#include "apiwrap/StdControl.h"
#include <array>
#include <Windows.h>

/*!
*/
Expand All @@ -32,3 +34,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);
EXPECT_STREQ(s.c_str(), text);
DestroyWindow(hwnd);
}

0 comments on commit fd8966f

Please sign in to comment.