Skip to content

Commit

Permalink
Merge pull request #1079 from berryzplus/feature/improve_equality_of_…
Browse files Browse the repository at this point in the history
…MYDEVMODE

MYDEVMODEの等価比較演算子の隠れバグを修正する
  • Loading branch information
berryzplus authored Oct 26, 2019
2 parents b32c6de + 51c2fd4 commit d7e1061
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sakura_core/print/CPrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ struct MYDEVMODE {
bool operator == (const MYDEVMODE& rhs) const noexcept {
if (this == &rhs) return true;
return m_bPrinterNotFound == rhs.m_bPrinterNotFound
&& 0 == wcsncmp(m_szPrinterDriverName, rhs.m_szPrinterDriverName, _countof(m_szPrinterDriverName) - 1)
&& 0 == wcsncmp(m_szPrinterDeviceName, rhs.m_szPrinterDeviceName, _countof(m_szPrinterDeviceName) - 1)
&& 0 == wcsncmp(m_szPrinterOutputName, rhs.m_szPrinterOutputName, _countof(m_szPrinterOutputName) - 1)
&& 0 == wcsncmp(m_szPrinterDriverName, rhs.m_szPrinterDriverName, _countof(m_szPrinterDriverName))
&& 0 == wcsncmp(m_szPrinterDeviceName, rhs.m_szPrinterDeviceName, _countof(m_szPrinterDeviceName))
&& 0 == wcsncmp(m_szPrinterOutputName, rhs.m_szPrinterOutputName, _countof(m_szPrinterOutputName))
&& dmFields == rhs.dmFields
&& dmOrientation == rhs.dmOrientation
&& dmPaperSize == rhs.dmPaperSize
Expand All @@ -83,7 +83,7 @@ struct MYDEVMODE {
&& dmYResolution == rhs.dmYResolution
&& dmTTOption == rhs.dmTTOption
&& dmCollate == rhs.dmCollate
&& 0 == wcsncmp(dmFormName, rhs.dmFormName, _countof(dmFormName) - 1)
&& 0 == wcsncmp(dmFormName, rhs.dmFormName, _countof(dmFormName))
&& dmLogPixels == rhs.dmLogPixels
&& dmBitsPerPel == rhs.dmBitsPerPel
&& dmPelsWidth == rhs.dmPelsWidth
Expand Down
29 changes: 29 additions & 0 deletions tests/unittests/test-mydevmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,35 @@ TEST(MYDEVMODETest, operatorNotEqual)
EXPECT_EQ(myDevMode, value);
}

/*!
* @brief 否定の等価演算子のテスト
* 文字列メンバの末尾(通常はNUL文字)が異なるパターンを検出できるかチェックする
*/
TEST(MYDEVMODETest, operatorNotEqualAntiLazyCode)
{
// デフォルトで初期化
MYDEVMODE value, other;

// スタック変数のアドレスをchar*にキャストしてデータを書き替える
char* buf1 = reinterpret_cast<char*>(&value);
::memset(buf1, 'a', sizeof(MYDEVMODE));
char* buf2 = reinterpret_cast<char*>(&other);
::memset(buf2, 'a', sizeof(MYDEVMODE));

// まったく同じなので等価になる
EXPECT_TRUE(value == other);
EXPECT_FALSE(value != other);
EXPECT_EQ(other, value);

// 文字列メンバをNUL終端する
value.m_szPrinterDriverName[_countof(value.m_szPrinterDriverName) - 1] = 0;

// NUL終端された文字列 != NUL終端されてない文字列、となるはず。
EXPECT_FALSE(value == other);
EXPECT_TRUE(value != other);
EXPECT_NE(other, value);
}

/*!
* @brief 等価比較演算子が一般保護違反を犯さないことを保証する非機能要件テスト
*
Expand Down

0 comments on commit d7e1061

Please sign in to comment.