Skip to content

Commit 889f15e

Browse files
Fix some compilation errors in windows.cpp (#5965)
`_strncpy_s_l()` is apparently only available in non-unicode builds, but the other code relies on `UNICODE`.
1 parent 1a5ba4a commit 889f15e

3 files changed

Lines changed: 76 additions & 56 deletions

File tree

.github/workflows/CI-windows.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,9 @@ jobs:
197197
..\..\cppcheck --dump naming_test.cpp || exit /b !errorlevel!
198198
python3 ..\naming.py --var='[a-z].*' --function='[a-z].*' naming_test.cpp.dump || exit /b !errorlevel!
199199
200+
- name: Check Windows test syntax
201+
if: matrix.config == 'debug'
202+
run: |
203+
cd test\cfg
204+
cl.exe windows.cpp -DUNICODE=1 -D_UNICODE=1 /Zs || exit /b !errorlevel!
205+
cl.exe mfc.cpp /EHsc /Zs || exit /b !errorlevel!

test/cfg/mfc.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
#include <afxwin.h>
55

66

7-
class MyClass : public CObject {
8-
DECLARE_DYNAMIC(MyClass)
9-
DECLARE_DYNCREATE(MyClass)
10-
DECLARE_SERIAL(MyClass)
7+
class MyClass1 : public CObject {
8+
DECLARE_DYNAMIC(MyClass1)
119
public:
12-
MyClass() {}
10+
MyClass1() {}
1311
};
14-
IMPLEMENT_DYNAMIC(MyClass, CObject)
15-
IMPLEMENT_DYNCREATE(MyClass, CObject)
16-
IMPLEMENT_SERIAL(MyClass,CObject, 42)
12+
IMPLEMENT_DYNAMIC(MyClass1, CObject)
13+
14+
class MyClass2 : public CObject {
15+
DECLARE_DYNCREATE(MyClass2)
16+
public:
17+
MyClass2() {}
18+
};
19+
IMPLEMENT_DYNCREATE(MyClass2, CObject)
20+
21+
class MyClass3 : public CObject {
22+
DECLARE_SERIAL(MyClass3)
23+
public:
24+
MyClass3() {}
25+
};
26+
IMPLEMENT_SERIAL(MyClass3, CObject, 42)

test/cfg/windows.cpp

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <ctime>
1616
#include <memory.h>
1717
#include <mbstring.h>
18+
#include <tchar.h>
1819
#include <wchar.h>
1920
#include <atlstr.h>
2021
#include <string>
@@ -54,17 +55,19 @@ int stringCompare_mbscmp(const unsigned char *string1, const unsigned char *stri
5455
{
5556
// cppcheck-suppress stringCompare
5657
(void) _mbscmp(string1, string1);
57-
// cppcheck-suppress staticStringCompare
58-
(void) _mbscmp("x", "x");
58+
const unsigned char x[] = "x";
59+
// cppcheck-suppress stringCompare
60+
(void) _mbscmp(x, x);
5961
return _mbscmp(string1, string2);
6062
}
6163

6264
int stringCompare_mbscmp_l(const unsigned char *string1, const unsigned char *string2, _locale_t locale)
6365
{
6466
// cppcheck-suppress stringCompare
6567
(void) _mbscmp_l(string1, string1, locale);
66-
// cppcheck-suppress staticStringCompare
67-
(void) _mbscmp_l("x", "x", locale);
68+
const unsigned char x[] = "x";
69+
// cppcheck-suppress stringCompare
70+
(void) _mbscmp_l(x, x, locale);
6871
return _mbscmp_l(string1, string2, locale);
6972
}
7073

@@ -99,7 +102,7 @@ void invalidFunctionArg__fseeki64(FILE* stream, __int64 offset, int origin)
99102
(void)_fseeki64(stream, offset, SEEK_END);
100103
}
101104

102-
void invalidFunctionArgBool__fseeki64(FILE* stream, __int64 offset, int origin)
105+
void invalidFunctionArgBool__fseeki64(FILE* stream, __int64 offset)
103106
{
104107
// cppcheck-suppress invalidFunctionArgBool
105108
(void)_fseeki64(stream, offset, true);
@@ -115,7 +118,7 @@ unsigned char * overlappingWriteFunction__mbscat(unsigned char *src, unsigned ch
115118
return _mbscat(src, src);
116119
}
117120

118-
unsigned char * overlappingWriteFunction__memccpy(const unsigned char *src, unsigned char *dest, int c, size_t count)
121+
void* overlappingWriteFunction__memccpy(const unsigned char *src, unsigned char *dest, int c, size_t count)
119122
{
120123
// No warning shall be shown:
121124
(void)_memccpy(dest, src, c, count);
@@ -142,7 +145,7 @@ void overlappingWriteFunction__swab(char *src, char *dest, int n)
142145
_swab(src, src+3, 4);
143146
}
144147

145-
SYSTEM_INFO uninitvar_GetSystemInfo(char * envstr)
148+
SYSTEM_INFO uninitvar_GetSystemInfo()
146149
{
147150
// No warning is expected
148151
SYSTEM_INFO SystemInfo;
@@ -238,13 +241,13 @@ void validCode()
238241
hSemaphore2 = CreateSemaphoreEx(NULL, 0, 1, NULL, 0, SEMAPHORE_ALL_ACCESS);
239242
CloseHandle(hSemaphore2);
240243
HANDLE hSemaphore3;
241-
hSemaphore3 = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, "sem");
244+
hSemaphore3 = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, L"sem");
242245
CloseHandle(hSemaphore3);
243246

244247
// Valid lstrcat usage, but with warning because it is deprecated
245248
char buf[30] = "hello world";
246-
// cppcheck-suppress lstrcatCalled
247-
lstrcat(buf, "test");
249+
// cppcheck-suppress lstrcatACalled
250+
lstrcatA(buf, "test");
248251

249252
// cppcheck-suppress strlwrCalled
250253
strlwr(buf);
@@ -255,14 +258,14 @@ void validCode()
255258
HANDLE hMutex1;
256259
hMutex1 = CreateMutex(NULL, TRUE, NULL);
257260
if (hMutex1) {
258-
ReleaseMutex(hMutex);
261+
ReleaseMutex(hMutex1);
259262
}
260263
CloseHandle(hMutex1);
261264
HANDLE hMutex2;
262265
hMutex2 = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS);
263266
CloseHandle(hMutex2);
264267
HANDLE hMutex3;
265-
hMutex3 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "sem");
268+
hMutex3 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, _T("sem"));
266269
CloseHandle(hMutex3);
267270

268271
// Valid Module usage, no leaks, valid arguments
@@ -334,12 +337,12 @@ void validCode()
334337
_tprintf(TEXT("%s"), bufTC);
335338
_stprintf(bufTC, TEXT("%d"), 1);
336339
_tprintf(TEXT("%s"), bufTC);
337-
_stprintf(bufTC, _countof(bufTC), TEXT("%d"), 2);
340+
_stprintf(bufTC, TEXT("%d"), 2);
338341
_tprintf(TEXT("%s"), bufTC);
339342

340343
GetUserName(NULL, &dwordInit);
341344
dwordInit = 10;
342-
GetUserName(bufTC, _countof(bufTC));
345+
GetUserName(bufTC, &dwordInit);
343346

344347
WSADATA wsaData = {0};
345348
WSAStartup(2, &wsaData);
@@ -460,7 +463,7 @@ void bufferAccessOutOfBounds()
460463
// cppcheck-suppress bufferAccessOutOfBounds
461464
FillMemory(byteBuf, sizeof(byteBuf)+1, 0x01);
462465

463-
char * pAlloc1 = _malloca(32);
466+
char * pAlloc1 = static_cast<char*>(_malloca(32));
464467
memset(pAlloc1, 0, 32);
465468
// cppcheck-suppress bufferAccessOutOfBounds
466469
memset(pAlloc1, 0, 33);
@@ -469,12 +472,12 @@ void bufferAccessOutOfBounds()
469472

470473
void mismatchAllocDealloc()
471474
{
472-
char * pChar = _aligned_malloc(100, 2);
475+
char * pChar = static_cast<char*>(_aligned_malloc(100, 2));
473476
// cppcheck-suppress mismatchAllocDealloc
474477
free(pChar);
475478

476479
// cppcheck-suppress unusedAllocatedMemory
477-
pChar = _malloca(32);
480+
pChar = static_cast<char*>(_malloca(32));
478481
// cppcheck-suppress mismatchAllocDealloc
479482
_aligned_free(pChar);
480483
}
@@ -488,8 +491,8 @@ void nullPointer()
488491

489492
// cppcheck-suppress lstrcatCalled
490493
// cppcheck-suppress nullPointer
491-
lstrcat(NULL, "test");
492-
char buf[10] = "\0";
494+
lstrcat(NULL, _T("test"));
495+
TCHAR buf[10] = _T("\0");
493496
// cppcheck-suppress lstrcatCalled
494497
// cppcheck-suppress nullPointer
495498
lstrcat(buf, NULL);
@@ -552,9 +555,9 @@ void nullPointer()
552555
// cppcheck-suppress nullPointer
553556
getpeername(socketInit, &sockaddrUninit, pIntNull);
554557
// cppcheck-suppress nullPointer
555-
getsockopt(sockInit, 1, 2, NULL, &intInit);
558+
getsockopt(socketInit, 1, 2, NULL, &intInit);
556559
// cppcheck-suppress nullPointer
557-
getsockopt(sockInit, 1, 2, charArray, pIntNull);
560+
getsockopt(socketInit, 1, 2, charArray, pIntNull);
558561
}
559562

560563
void memleak_malloca()
@@ -568,7 +571,7 @@ void memleak_AllocateAndInitializeSid()
568571
{
569572
PSID pEveryoneSID = NULL;
570573
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
571-
AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID)
574+
AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID);
572575
// TODO: enable when #6994 is implemented cppcheck-suppress memleak
573576
}
574577

@@ -599,11 +602,11 @@ void memleak_dupenv_s() // #10646
599602
char* pValue;
600603
size_t len;
601604
errno_t err = _dupenv_s(&pValue, &len, "pathext");
602-
if (err) return -1;
605+
if (err) return;
603606
printf("pathext = %s\n", pValue);
604607
free(pValue);
605608
err = _dupenv_s(&pValue, &len, "nonexistentvariable");
606-
if (err) return -1;
609+
if (err) return;
607610
printf("nonexistentvariable = %s\n", pValue);
608611
// cppcheck-suppress memleak
609612
}
@@ -628,7 +631,7 @@ void resourceLeak_OpenSemaphore()
628631
{
629632
HANDLE hSemaphore;
630633
// cppcheck-suppress unreadVariable
631-
hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, "sem");
634+
hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, _T("sem"));
632635
// cppcheck-suppress resourceLeak
633636
}
634637

@@ -644,15 +647,15 @@ void resourceLeak_CreateMutexEx()
644647
{
645648
HANDLE hMutex;
646649
// cppcheck-suppress unreadVariable
647-
hMutex = CreateMutexEx(NULL, "sem", 0, MUTEX_ALL_ACCESS);
650+
hMutex = CreateMutexEx(NULL, _T("sem"), 0, MUTEX_ALL_ACCESS);
648651
// cppcheck-suppress resourceLeak
649652
}
650653

651654
void resourceLeak_OpenMutex()
652655
{
653656
HANDLE hMutex;
654657
// cppcheck-suppress unreadVariable
655-
hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, "sem");
658+
hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, _T("sem"));
656659
// cppcheck-suppress resourceLeak
657660
}
658661

@@ -661,8 +664,8 @@ void resourceLeak_LoadLibrary()
661664
HINSTANCE hInstLib;
662665
hInstLib = ::LoadLibrary(L"My.dll");
663666
typedef BOOL (WINAPI *fpFunc)();
664-
// cppcheck-suppress unreadVariable
665-
fpFunc pFunc = GetProcAddress(hInstLib, "name");
667+
// cppcheck-suppress [unreadVariable, cstyleCast]
668+
fpFunc pFunc = (fpFunc)GetProcAddress(hInstLib, "name");
666669
// cppcheck-suppress resourceLeak
667670
}
668671

@@ -698,7 +701,7 @@ void resourceLeak_socket()
698701
// cppcheck-suppress resourceLeak
699702
}
700703

701-
void ignoredReturnValue()
704+
void ignoredReturnValue(FILE* fp)
702705
{
703706
// cppcheck-suppress leakReturnValNotUsed
704707
CreateSemaphoreW(NULL, 0, 1, NULL);
@@ -744,14 +747,14 @@ void ignoredReturnValue()
744747
GetProcessHeap();
745748
// cppcheck-suppress leakReturnValNotUsed
746749
HeapAlloc(GetProcessHeap(), 0, 10);
747-
// cppcheck-suppress leakReturnValNotUsed
748-
HeapReAlloc(GetProcessHeap(), 0, 1, 0);
750+
// cppcheck-suppress [leakReturnValNotUsed, nullPointer]
751+
HeapReAlloc(GetProcessHeap(), 0, nullptr, 0);
749752

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

753756
// cppcheck-suppress ignoredReturnValue
754-
_fileno(stdio);
757+
_fileno(fp);
755758

756759
// cppcheck-suppress lstrlenCalled
757760
// cppcheck-suppress ignoredReturnValue
@@ -765,7 +768,7 @@ void invalidFunctionArg()
765768
hSemaphore = CreateSemaphore(NULL, 0, 0, NULL);
766769
CloseHandle(hSemaphore);
767770
// cppcheck-suppress invalidFunctionArgBool
768-
hSemaphore = CreateSemaphore(NULL, 0, 1, true);
771+
hSemaphore = CreateSemaphore(NULL, 0, 1, false);
769772
CloseHandle(hSemaphore);
770773
// cppcheck-suppress invalidFunctionArg
771774
hSemaphore = CreateSemaphoreEx(NULL, 0, 0, NULL, 0, SEMAPHORE_ALL_ACCESS);
@@ -779,15 +782,15 @@ void invalidFunctionArg()
779782
hMutex = CreateMutex(NULL, TRUE, false);
780783
CloseHandle(hMutex);
781784
// cppcheck-suppress invalidFunctionArgBool
782-
hMutex = CreateMutex(NULL, FALSE, true);
785+
hMutex = CreateMutex(NULL, FALSE, false);
783786
CloseHandle(hMutex);
784787
// cppcheck-suppress invalidFunctionArg
785788
hMutex = CreateMutexEx(NULL, NULL, 3, MUTEX_ALL_ACCESS);
786789
CloseHandle(hMutex);
787790

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

793796
// cppcheck-suppress invalidFunctionArg
@@ -806,13 +809,13 @@ void uninitvar()
806809
// cppcheck-suppress uninitvar
807810
CloseHandle(hSemaphore);
808811

809-
char buf[10];
812+
TCHAR buf[10];
810813
// cppcheck-suppress lstrcatCalled
811814
// cppcheck-suppress uninitvar
812-
lstrcat(buf, "test");
813-
buf[0] = '\0';
815+
lstrcat(buf, _T("test"));
816+
buf[0] = _T('\0');
814817
// cppcheck-suppress constVariable
815-
char buf2[2];
818+
TCHAR buf2[2];
816819
// cppcheck-suppress lstrcatCalled
817820
// cppcheck-suppress uninitvar
818821
lstrcat(buf, buf2);
@@ -848,9 +851,9 @@ void uninitvar()
848851
// cppcheck-suppress uninitvar
849852
SetLastError(dwordUninit);
850853

851-
DWORD dwordUninit;
854+
DWORD dwordUninit2;
852855
// cppcheck-suppress uninitvar
853-
GetUserName(NULL, &dwordUninit);
856+
GetUserName(NULL, &dwordUninit2);
854857

855858
FILE *pFileUninit;
856859
// cppcheck-suppress uninitvar
@@ -1064,7 +1067,7 @@ unsigned char * nullPointer_mbscat(unsigned char *strDestination, const unsigned
10641067
}
10651068

10661069
// errno_t _mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource );
1067-
error_t uninitvar_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
1070+
errno_t uninitvar_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
10681071
{
10691072
unsigned char *uninit_strDestination;
10701073
size_t uninit_numberOfElements;
@@ -1082,7 +1085,7 @@ error_t uninitvar_mbscat_s(unsigned char *strDestination, size_t numberOfElement
10821085
}
10831086

10841087
// errno_t _mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource );
1085-
error_t nullPointer_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
1088+
errno_t nullPointer_mbscat_s(unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource)
10861089
{
10871090
// cppcheck-suppress nullPointer
10881091
(void)_mbscat_s(0, numberOfElements, strSource);
@@ -1093,8 +1096,9 @@ error_t nullPointer_mbscat_s(unsigned char *strDestination, size_t numberOfEleme
10931096
return _mbscat_s(strDestination, numberOfElements, strSource);
10941097
}
10951098

1099+
#if !UNICODE
10961100
// errno_t _strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale);
1097-
error_t uninitvar__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
1101+
errno_t uninitvar__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
10981102
{
10991103
size_t uninit_numberOfElements;
11001104
const char *uninit_strSource;
@@ -1114,8 +1118,7 @@ error_t uninitvar__strncpy_s_l(char *strDest, size_t numberOfElements, const cha
11141118
return _strncpy_s_l(strDest, numberOfElements, strSource, count, locale);
11151119
}
11161120

1117-
// errno_t _strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale);
1118-
error_t nullPointer__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
1121+
errno_t nullPointer__strncpy_s_l(char *strDest, size_t numberOfElements, const char *strSource, size_t count, _locale_t locale)
11191122
{
11201123
// cppcheck-suppress nullPointer
11211124
(void)_strncpy_s_l(0, numberOfElements, strSource, count, locale);
@@ -1125,6 +1128,7 @@ error_t nullPointer__strncpy_s_l(char *strDest, size_t numberOfElements, const c
11251128
// no warning shall be shown for
11261129
return _strncpy_s_l(strDest, numberOfElements, strSource, count, locale);
11271130
}
1131+
#endif
11281132

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

11501154
my_struct_t my_struct;
11511155
// cppcheck-suppress invalidPrintfArgType_sint
1152-
my_struct.st.Format("%d", d);
1156+
my_struct.st.Format(_T("%d"), d);
11531157
}
11541158

11551159
BOOL MyEnableWindow(HWND hWnd, BOOL bEnable) {

0 commit comments

Comments
 (0)