Skip to content

Commit 6d800de

Browse files
authored
Create test1.cpp
1 parent f53adca commit 6d800de

File tree

1 file changed

+95
-0
lines changed
  • cpp/ql/test/experimental/query-tests/Security/CWE/CWE-125/semmle/tests

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#define CP_ACP 1
2+
#define CP_UTF8 1
3+
#define WC_COMPOSITECHECK 1
4+
#define NULL 0
5+
typedef unsigned int UINT;
6+
typedef unsigned long DWORD, *PDWORD, *LPDWORD;
7+
typedef char CHAR;
8+
#define CONST const
9+
typedef wchar_t WCHAR;
10+
11+
typedef CHAR *LPSTR;
12+
typedef CONST CHAR *LPCSTR;
13+
typedef CONST WCHAR *LPCWSTR;
14+
15+
typedef int BOOL;
16+
typedef BOOL *LPBOOL;
17+
18+
19+
int WideCharToMultiByte(UINT CodePage,DWORD dwFlags,LPCWSTR lpWideCharStr,int cchWideChar,LPSTR lpMultiByteStr,int cbMultiByte,LPCWSTR lpDefaultChar,LPBOOL lpUsedDefaultChar);
20+
int MultiByteToWideChar(UINT CodePage,DWORD dwFlags,LPCSTR lpMultiByteStr,int cbMultiByte,LPCWSTR lpWideCharStr,int cchWideChar);
21+
22+
int printf ( const char * format, ... );
23+
typedef unsigned int size_t;
24+
void* calloc (size_t num, size_t size);
25+
void* malloc (size_t size);
26+
27+
void badTest1(void *src, int size) {
28+
WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, (LPSTR)src, size, 0, 0); // BAD
29+
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, (LPCWSTR)src, 30); // BAD
30+
}
31+
void goodTest2(){
32+
wchar_t src[] = L"0123456789ABCDEF";
33+
char dst[16];
34+
int res = WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, 16, NULL, NULL); // GOOD
35+
if (res == sizeof(dst)) {
36+
dst[res-1] = NULL;
37+
} else {
38+
dst[res] = NULL;
39+
}
40+
printf("%s\n", dst);
41+
}
42+
void badTest2(){
43+
wchar_t src[] = L"0123456789ABCDEF";
44+
char dst[16];
45+
WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, 16, NULL, NULL); // BAD
46+
printf("%s\n", dst);
47+
}
48+
void goodTest3(){
49+
char src[] = "0123456789ABCDEF";
50+
int size = MultiByteToWideChar(CP_UTF8, 0, src,sizeof(src),NULL,0);
51+
wchar_t * dst = (wchar_t*)calloc(size + 1, sizeof(wchar_t));
52+
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // GOOD
53+
}
54+
void badTest3(){
55+
char src[] = "0123456789ABCDEF";
56+
int size = MultiByteToWideChar(CP_UTF8, 0, src,sizeof(src),NULL,0);
57+
wchar_t * dst = (wchar_t*)calloc(size + 1, 1);
58+
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // BAD
59+
}
60+
void goodTest4(){
61+
char src[] = "0123456789ABCDEF";
62+
int size = MultiByteToWideChar(CP_UTF8, 0, src,sizeof(src),NULL,0);
63+
wchar_t * dst = (wchar_t*)malloc((size + 1)*sizeof(wchar_t));
64+
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // GOOD
65+
}
66+
void badTest4(){
67+
char src[] = "0123456789ABCDEF";
68+
int size = MultiByteToWideChar(CP_UTF8, 0, src,sizeof(src),NULL,0);
69+
wchar_t * dst = (wchar_t*)malloc(size + 1);
70+
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // BAD
71+
}
72+
int goodTest5(void *src){
73+
return WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, 0, 0, 0, 0); // GOOD
74+
}
75+
int badTest5 (void *src) {
76+
return WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, 0, 3, 0, 0); // BAD
77+
}
78+
void goodTest6(WCHAR *src)
79+
{
80+
int size;
81+
char dst[5] ="";
82+
size = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, src, -1, dst, 0, 0, 0);
83+
if(size>=sizeof(dst)){
84+
printf("buffer size error\n");
85+
return;
86+
}
87+
WideCharToMultiByte(CP_ACP, 0, src, -1, dst, sizeof(dst), 0, 0); // GOOD
88+
printf("%s\n", dst);
89+
}
90+
void badTest6(WCHAR *src)
91+
{
92+
char dst[5] ="";
93+
WideCharToMultiByte(CP_ACP, 0, src, -1, dst, 260, 0, 0); // BAD
94+
printf("%s\n", dst);
95+
}

0 commit comments

Comments
 (0)