From ed3cd2929ecf1a37146d1b2388a9c598d3878f8e Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 14 Mar 2018 15:01:37 +0100 Subject: [PATCH] Windows library: Enhance functions, add tests (#1117) Enhance *Equal/*Compare/*Copy/*Move/*Zero/*Fill memory functions to catch buffer access out of bounds errors and ignored return values. Replaced some function configuration by according defines as it is done in windows to avoid redundant (and error prone) configurations. --- cfg/windows.cfg | 172 ++++++++----------------------------------- test/cfg/windows.cpp | 47 ++++++++++++ 2 files changed, 78 insertions(+), 141 deletions(-) diff --git a/cfg/windows.cfg b/cfg/windows.cfg index 6cb0b76c8..d89b29b92 100644 --- a/cfg/windows.cfg +++ b/cfg/windows.cfg @@ -1204,62 +1204,45 @@ AllocateAndInitializeSid FreeSid + false + + + + 0: + false + - - - - 0: - - - - false - - - - - - - - - - - 0: - - - - false - - - - - - - - + @@ -1279,41 +1262,18 @@ + - - false - - - - - - - 0: - - - - - - - false - - - - - - - - - - - 0: - - + false + + @@ -1330,87 +1290,7 @@ 0: - - - false - - - - - - - 0: - - - - - false - - - - - - - - - - - 0: - - - - false - - - - - - - 0: - - - - - - - false - - - - - - - - - - - 0: - - - - false - - - - - - - 0: - - - - false - - - - - - - 0: - + true @@ -4964,4 +4844,14 @@ HFONT CreateFont( + + + + + + + + + + diff --git a/test/cfg/windows.cpp b/test/cfg/windows.cpp index d598a71ca..d19adf312 100644 --- a/test/cfg/windows.cpp +++ b/test/cfg/windows.cpp @@ -131,6 +131,21 @@ void validCode() } WSACleanup(); + bool boolVar; + uint8_t byteBuf[5] = {0}; + uint8_t byteBuf2[10] = {0}; + boolVar = RtlEqualMemory(byteBuf, byteBuf2, sizeof(byteBuf)); + if (boolVar) {} + boolVar = RtlCompareMemory(byteBuf, byteBuf2, sizeof(byteBuf)); + if (boolVar) {} + RtlMoveMemory(byteBuf, byteBuf2, sizeof(byteBuf)); + RtlCopyMemory(byteBuf, byteBuf2, sizeof(byteBuf)); + RtlZeroMemory(byteBuf, sizeof(byteBuf)); + ZeroMemory(byteBuf, sizeof(byteBuf)); + RtlSecureZeroMemory(byteBuf, sizeof(byteBuf)); + SecureZeroMemory(byteBuf, sizeof(byteBuf)); + RtlFillMemory(byteBuf, sizeof(byteBuf), 0xff); + // Valid Library usage, no leaks, valid arguments HINSTANCE hInstLib = LoadLibrary(L"My.dll"); FreeLibrary(hInstLib); @@ -157,6 +172,38 @@ void bufferAccessOutOfBounds() // cppcheck-suppress arrayIndexOutOfBounds buf[i] = L'\0'; } + + uint8_t byteBuf[5] = {0}; + uint8_t byteBuf2[10] = {0}; + // TODO ticket #8412 cppcheck-suppress ignoredReturnValue + // cppcheck-suppress bufferAccessOutOfBounds + RtlEqualMemory(byteBuf, byteBuf2, 20); + // cppcheck-suppress ignoredReturnValue + // cppcheck-suppress bufferAccessOutOfBounds + RtlCompareMemory(byteBuf, byteBuf2, 20); + // cppcheck-suppress bufferAccessOutOfBounds + RtlMoveMemory(byteBuf, byteBuf2, 20); + // cppcheck-suppress redundantCopy + // cppcheck-suppress bufferAccessOutOfBounds + MoveMemory(byteBuf, byteBuf2, 20); + // cppcheck-suppress redundantCopy + // cppcheck-suppress bufferAccessOutOfBounds + RtlCopyMemory(byteBuf, byteBuf2, 20); + // cppcheck-suppress redundantCopy + // cppcheck-suppress bufferAccessOutOfBounds + CopyMemory(byteBuf, byteBuf2, 20); + // cppcheck-suppress bufferAccessOutOfBounds + RtlZeroMemory(byteBuf, sizeof(byteBuf)+1); + // cppcheck-suppress bufferAccessOutOfBounds + ZeroMemory(byteBuf, sizeof(byteBuf)+1); + // cppcheck-suppress bufferAccessOutOfBounds + RtlSecureZeroMemory(byteBuf, sizeof(byteBuf)+1); + // cppcheck-suppress bufferAccessOutOfBounds + SecureZeroMemory(byteBuf, sizeof(byteBuf)+1); + // cppcheck-suppress bufferAccessOutOfBounds + RtlFillMemory(byteBuf, sizeof(byteBuf)+1, 0x01); + // cppcheck-suppress bufferAccessOutOfBounds + FillMemory(byteBuf, sizeof(byteBuf)+1, 0x01); } void nullPointer()