Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some compilation errors in windows.cpp #5965

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/CI-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,9 @@ jobs:
..\..\cppcheck --dump naming_test.cpp || exit /b !errorlevel!
python3 ..\naming.py --var='[a-z].*' --function='[a-z].*' naming_test.cpp.dump || exit /b !errorlevel!

- name: Check Windows test syntax
if: matrix.config == 'debug'
run: |
cd test\cfg
cl.exe windows.cpp -DUNICODE=1 -D_UNICODE=1 /Zs || exit /b !errorlevel!
cl.exe mfc.cpp /EHsc /Zs || exit /b !errorlevel!
26 changes: 18 additions & 8 deletions test/cfg/mfc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
#include <afxwin.h>


class MyClass : public CObject {
DECLARE_DYNAMIC(MyClass)
DECLARE_DYNCREATE(MyClass)
DECLARE_SERIAL(MyClass)
class MyClass1 : public CObject {
DECLARE_DYNAMIC(MyClass1)
public:
MyClass() {}
MyClass1() {}
};
IMPLEMENT_DYNAMIC(MyClass, CObject)
IMPLEMENT_DYNCREATE(MyClass, CObject)
IMPLEMENT_SERIAL(MyClass,CObject, 42)
IMPLEMENT_DYNAMIC(MyClass1, CObject)

class MyClass2 : public CObject {
DECLARE_DYNCREATE(MyClass2)
public:
MyClass2() {}
};
IMPLEMENT_DYNCREATE(MyClass2, CObject)

class MyClass3 : public CObject {
DECLARE_SERIAL(MyClass3)
public:
MyClass3() {}
};
IMPLEMENT_SERIAL(MyClass3, CObject, 42)
100 changes: 52 additions & 48 deletions test/cfg/windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <ctime>
#include <memory.h>
#include <mbstring.h>
#include <tchar.h>
#include <wchar.h>
#include <atlstr.h>
#include <string>
Expand Down Expand Up @@ -54,17 +55,19 @@ int stringCompare_mbscmp(const unsigned char *string1, const unsigned char *stri
{
// cppcheck-suppress stringCompare
(void) _mbscmp(string1, string1);
// cppcheck-suppress staticStringCompare
(void) _mbscmp("x", "x");
const unsigned char x[] = "x";
// cppcheck-suppress stringCompare
(void) _mbscmp(x, x);
return _mbscmp(string1, string2);
}

int stringCompare_mbscmp_l(const unsigned char *string1, const unsigned char *string2, _locale_t locale)
{
// cppcheck-suppress stringCompare
(void) _mbscmp_l(string1, string1, locale);
// cppcheck-suppress staticStringCompare
(void) _mbscmp_l("x", "x", locale);
const unsigned char x[] = "x";
// cppcheck-suppress stringCompare
(void) _mbscmp_l(x, x, locale);
return _mbscmp_l(string1, string2, locale);
}

Expand Down Expand Up @@ -99,7 +102,7 @@ void invalidFunctionArg__fseeki64(FILE* stream, __int64 offset, int origin)
(void)_fseeki64(stream, offset, SEEK_END);
}

void invalidFunctionArgBool__fseeki64(FILE* stream, __int64 offset, int origin)
void invalidFunctionArgBool__fseeki64(FILE* stream, __int64 offset)
{
// cppcheck-suppress invalidFunctionArgBool
(void)_fseeki64(stream, offset, true);
Expand All @@ -115,7 +118,7 @@ unsigned char * overlappingWriteFunction__mbscat(unsigned char *src, unsigned ch
return _mbscat(src, src);
}

unsigned char * overlappingWriteFunction__memccpy(const unsigned char *src, unsigned char *dest, int c, size_t count)
void* overlappingWriteFunction__memccpy(const unsigned char *src, unsigned char *dest, int c, size_t count)
{
// No warning shall be shown:
(void)_memccpy(dest, src, c, count);
Expand All @@ -142,7 +145,7 @@ void overlappingWriteFunction__swab(char *src, char *dest, int n)
_swab(src, src+3, 4);
}

SYSTEM_INFO uninitvar_GetSystemInfo(char * envstr)
SYSTEM_INFO uninitvar_GetSystemInfo()
{
// No warning is expected
SYSTEM_INFO SystemInfo;
Expand Down Expand Up @@ -238,13 +241,13 @@ void validCode()
hSemaphore2 = CreateSemaphoreEx(NULL, 0, 1, NULL, 0, SEMAPHORE_ALL_ACCESS);
CloseHandle(hSemaphore2);
HANDLE hSemaphore3;
hSemaphore3 = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, "sem");
hSemaphore3 = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, L"sem");
CloseHandle(hSemaphore3);

// Valid lstrcat usage, but with warning because it is deprecated
char buf[30] = "hello world";
// cppcheck-suppress lstrcatCalled
lstrcat(buf, "test");
// cppcheck-suppress lstrcatACalled
lstrcatA(buf, "test");

// cppcheck-suppress strlwrCalled
strlwr(buf);
Expand All @@ -255,14 +258,14 @@ void validCode()
HANDLE hMutex1;
hMutex1 = CreateMutex(NULL, TRUE, NULL);
if (hMutex1) {
ReleaseMutex(hMutex);
ReleaseMutex(hMutex1);
}
CloseHandle(hMutex1);
HANDLE hMutex2;
hMutex2 = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS);
CloseHandle(hMutex2);
HANDLE hMutex3;
hMutex3 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "sem");
hMutex3 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, _T("sem"));
CloseHandle(hMutex3);

// Valid Module usage, no leaks, valid arguments
Expand Down Expand Up @@ -334,12 +337,12 @@ void validCode()
_tprintf(TEXT("%s"), bufTC);
_stprintf(bufTC, TEXT("%d"), 1);
_tprintf(TEXT("%s"), bufTC);
_stprintf(bufTC, _countof(bufTC), TEXT("%d"), 2);
_stprintf(bufTC, TEXT("%d"), 2);
_tprintf(TEXT("%s"), bufTC);

GetUserName(NULL, &dwordInit);
dwordInit = 10;
GetUserName(bufTC, _countof(bufTC));
GetUserName(bufTC, &dwordInit);

WSADATA wsaData = {0};
WSAStartup(2, &wsaData);
Expand Down Expand Up @@ -460,7 +463,7 @@ void bufferAccessOutOfBounds()
// cppcheck-suppress bufferAccessOutOfBounds
FillMemory(byteBuf, sizeof(byteBuf)+1, 0x01);

char * pAlloc1 = _malloca(32);
char * pAlloc1 = static_cast<char*>(_malloca(32));
memset(pAlloc1, 0, 32);
// cppcheck-suppress bufferAccessOutOfBounds
memset(pAlloc1, 0, 33);
Expand All @@ -469,12 +472,12 @@ void bufferAccessOutOfBounds()

void mismatchAllocDealloc()
{
char * pChar = _aligned_malloc(100, 2);
char * pChar = static_cast<char*>(_aligned_malloc(100, 2));
// cppcheck-suppress mismatchAllocDealloc
free(pChar);

// cppcheck-suppress unusedAllocatedMemory
pChar = _malloca(32);
pChar = static_cast<char*>(_malloca(32));
// cppcheck-suppress mismatchAllocDealloc
_aligned_free(pChar);
}
Expand All @@ -488,8 +491,8 @@ void nullPointer()

// cppcheck-suppress lstrcatCalled
// cppcheck-suppress nullPointer
lstrcat(NULL, "test");
char buf[10] = "\0";
lstrcat(NULL, _T("test"));
TCHAR buf[10] = _T("\0");
// cppcheck-suppress lstrcatCalled
// cppcheck-suppress nullPointer
lstrcat(buf, NULL);
Expand Down Expand Up @@ -552,9 +555,9 @@ void nullPointer()
// cppcheck-suppress nullPointer
getpeername(socketInit, &sockaddrUninit, pIntNull);
// cppcheck-suppress nullPointer
getsockopt(sockInit, 1, 2, NULL, &intInit);
getsockopt(socketInit, 1, 2, NULL, &intInit);
// cppcheck-suppress nullPointer
getsockopt(sockInit, 1, 2, charArray, pIntNull);
getsockopt(socketInit, 1, 2, charArray, pIntNull);
}

void memleak_malloca()
Expand All @@ -568,7 +571,7 @@ void memleak_AllocateAndInitializeSid()
{
PSID pEveryoneSID = NULL;
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID)
AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID);
// TODO: enable when #6994 is implemented cppcheck-suppress memleak
}

Expand Down Expand Up @@ -599,11 +602,11 @@ void memleak_dupenv_s() // #10646
char* pValue;
size_t len;
errno_t err = _dupenv_s(&pValue, &len, "pathext");
if (err) return -1;
if (err) return;
printf("pathext = %s\n", pValue);
free(pValue);
err = _dupenv_s(&pValue, &len, "nonexistentvariable");
if (err) return -1;
if (err) return;
printf("nonexistentvariable = %s\n", pValue);
// cppcheck-suppress memleak
}
Expand All @@ -628,7 +631,7 @@ void resourceLeak_OpenSemaphore()
{
HANDLE hSemaphore;
// cppcheck-suppress unreadVariable
hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, "sem");
hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, _T("sem"));
// cppcheck-suppress resourceLeak
}

Expand All @@ -644,15 +647,15 @@ void resourceLeak_CreateMutexEx()
{
HANDLE hMutex;
// cppcheck-suppress unreadVariable
hMutex = CreateMutexEx(NULL, "sem", 0, MUTEX_ALL_ACCESS);
hMutex = CreateMutexEx(NULL, _T("sem"), 0, MUTEX_ALL_ACCESS);
// cppcheck-suppress resourceLeak
}

void resourceLeak_OpenMutex()
{
HANDLE hMutex;
// cppcheck-suppress unreadVariable
hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, "sem");
hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, _T("sem"));
// cppcheck-suppress resourceLeak
}

Expand All @@ -661,8 +664,8 @@ void resourceLeak_LoadLibrary()
HINSTANCE hInstLib;
hInstLib = ::LoadLibrary(L"My.dll");
typedef BOOL (WINAPI *fpFunc)();
// cppcheck-suppress unreadVariable
fpFunc pFunc = GetProcAddress(hInstLib, "name");
// cppcheck-suppress [unreadVariable, cstyleCast]
fpFunc pFunc = (fpFunc)GetProcAddress(hInstLib, "name");
// cppcheck-suppress resourceLeak
}

Expand Down Expand Up @@ -698,7 +701,7 @@ void resourceLeak_socket()
// cppcheck-suppress resourceLeak
}

void ignoredReturnValue()
void ignoredReturnValue(FILE* fp)
{
// cppcheck-suppress leakReturnValNotUsed
CreateSemaphoreW(NULL, 0, 1, NULL);
Expand Down Expand Up @@ -744,14 +747,14 @@ void ignoredReturnValue()
GetProcessHeap();
// cppcheck-suppress leakReturnValNotUsed
HeapAlloc(GetProcessHeap(), 0, 10);
// cppcheck-suppress leakReturnValNotUsed
HeapReAlloc(GetProcessHeap(), 0, 1, 0);
// cppcheck-suppress [leakReturnValNotUsed, nullPointer]
HeapReAlloc(GetProcessHeap(), 0, nullptr, 0);

// cppcheck-suppress leakReturnValNotUsed
socket(1, 2, 3);

// cppcheck-suppress ignoredReturnValue
_fileno(stdio);
_fileno(fp);

// cppcheck-suppress lstrlenCalled
// cppcheck-suppress ignoredReturnValue
Expand All @@ -765,7 +768,7 @@ void invalidFunctionArg()
hSemaphore = CreateSemaphore(NULL, 0, 0, NULL);
CloseHandle(hSemaphore);
// cppcheck-suppress invalidFunctionArgBool
hSemaphore = CreateSemaphore(NULL, 0, 1, true);
hSemaphore = CreateSemaphore(NULL, 0, 1, false);
CloseHandle(hSemaphore);
// cppcheck-suppress invalidFunctionArg
hSemaphore = CreateSemaphoreEx(NULL, 0, 0, NULL, 0, SEMAPHORE_ALL_ACCESS);
Expand All @@ -779,15 +782,15 @@ void invalidFunctionArg()
hMutex = CreateMutex(NULL, TRUE, false);
CloseHandle(hMutex);
// cppcheck-suppress invalidFunctionArgBool
hMutex = CreateMutex(NULL, FALSE, true);
hMutex = CreateMutex(NULL, FALSE, false);
CloseHandle(hMutex);
// cppcheck-suppress invalidFunctionArg
hMutex = CreateMutexEx(NULL, NULL, 3, MUTEX_ALL_ACCESS);
CloseHandle(hMutex);

//Incorrect: 2. parameter to LoadLibraryEx() must be NULL
// cppcheck-suppress invalidFunctionArg
HINSTANCE hInstLib = LoadLibraryEx(L"My.dll", 1, 0);
HINSTANCE hInstLib = LoadLibraryEx(L"My.dll", HANDLE(1), 0);
FreeLibrary(hInstLib);

// cppcheck-suppress invalidFunctionArg
Expand All @@ -806,13 +809,13 @@ void uninitvar()
// cppcheck-suppress uninitvar
CloseHandle(hSemaphore);

char buf[10];
TCHAR buf[10];
// cppcheck-suppress lstrcatCalled
// cppcheck-suppress uninitvar
lstrcat(buf, "test");
buf[0] = '\0';
lstrcat(buf, _T("test"));
buf[0] = _T('\0');
// cppcheck-suppress constVariable
char buf2[2];
TCHAR buf2[2];
// cppcheck-suppress lstrcatCalled
// cppcheck-suppress uninitvar
lstrcat(buf, buf2);
Expand Down Expand Up @@ -848,9 +851,9 @@ void uninitvar()
// cppcheck-suppress uninitvar
SetLastError(dwordUninit);

DWORD dwordUninit;
DWORD dwordUninit2;
// cppcheck-suppress uninitvar
GetUserName(NULL, &dwordUninit);
GetUserName(NULL, &dwordUninit2);

FILE *pFileUninit;
// cppcheck-suppress uninitvar
Expand Down Expand Up @@ -1064,7 +1067,7 @@ unsigned char * nullPointer_mbscat(unsigned char *strDestination, const unsigned
}

// errno_t _mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource );
error_t uninitvar_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
errno_t uninitvar_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
{
unsigned char *uninit_strDestination;
size_t uninit_numberOfElements;
Expand All @@ -1082,7 +1085,7 @@ error_t uninitvar_mbscat_s(unsigned char *strDestination, size_t numberOfElement
}

// errno_t _mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource );
error_t nullPointer_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
errno_t nullPointer_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
{
// cppcheck-suppress nullPointer
(void)_mbscat_s(0, numberOfElements, strSource);
Expand All @@ -1093,8 +1096,9 @@ error_t nullPointer_mbscat_s(unsigned char *strDestination, size_t numberOfEleme
return _mbscat_s(strDestination, numberOfElements, strSource);
}

#if !UNICODE
// errno_t _strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale);
error_t uninitvar__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
errno_t uninitvar__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
{
size_t uninit_numberOfElements;
const char *uninit_strSource;
Expand All @@ -1114,8 +1118,7 @@ error_t uninitvar__strncpy_s_l(char *strDest, size_t numberOfElements, const cha
return _strncpy_s_l(strDest, numberOfElements, strSource, count, locale);
}

// errno_t _strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale);
error_t nullPointer__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
errno_t nullPointer__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
{
// cppcheck-suppress nullPointer
(void)_strncpy_s_l(0, numberOfElements, strSource, count, locale);
Expand All @@ -1125,6 +1128,7 @@ error_t nullPointer__strncpy_s_l(char *strDest, size_t numberOfElements, const c
// no warning shall be shown for
return _strncpy_s_l(strDest, numberOfElements, strSource, count, locale);
}
#endif

void GetShortPathName_validCode(const TCHAR* lpszPath)
{
Expand All @@ -1149,7 +1153,7 @@ void invalidPrintfArgType_StructMember(double d) { // #9672

my_struct_t my_struct;
// cppcheck-suppress invalidPrintfArgType_sint
my_struct.st.Format("%d", d);
my_struct.st.Format(_T("%d"), d);
}

BOOL MyEnableWindow(HWND hWnd, BOOL bEnable) {
Expand Down
Loading