Skip to content

Commit 1ba8364

Browse files
committed
CPP: Add more test cases.
1 parent 8856442 commit 1ba8364

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

  • cpp/ql/test/query-tests/Critical/OverflowCalculated
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// tests1.cpp
2+
3+
typedef unsigned int size_t;
4+
5+
char *strcpy(char *destination, const char *source);
6+
char *strcat(char *destination, const char *source);
7+
size_t strlen(const char *str);
8+
9+
namespace std
10+
{
11+
void *malloc(size_t size);
12+
void free(void *ptr);
13+
}
14+
15+
const char *str3global = "123";
16+
17+
void tests3(int case_num)
18+
{
19+
const char *str3local = "123";
20+
char *buffer = 0;
21+
22+
switch (case_num)
23+
{
24+
case 1:
25+
buffer = (char *)std::malloc(strlen(str3global)); // BAD [NOT DETECTED]
26+
strcpy(buffer, str3global);
27+
break;
28+
29+
case 2:
30+
buffer = (char *)std::malloc(strlen(str3local)); // BAD [NOT DETECTED]
31+
strcpy(buffer, str3local);
32+
break;
33+
34+
case 3:
35+
buffer = (char *)std::malloc(strlen(str3global) + 1); // GOOD
36+
strcpy(buffer, str3global);
37+
break;
38+
39+
case 4:
40+
buffer = (char *)std::malloc(strlen(str3local) + 1); // GOOD
41+
strcpy(buffer, str3local);
42+
break;
43+
}
44+
45+
if (buffer != 0)
46+
{
47+
std::free(buffer);
48+
}
49+
}
50+
51+
void test3b()
52+
{
53+
char *buffer = new char[strlen(str3global)]; // BAD [NOT DETECTED]
54+
55+
strcpy(buffer, str3global);
56+
57+
delete buffer;
58+
}
59+
60+
void test3c()
61+
{
62+
char *buffer = new char[10]; // BAD [NOT DETECTED]
63+
64+
strcpy(buffer, "123456");
65+
strcat(buffer, "123456");
66+
67+
delete buffer;
68+
}

0 commit comments

Comments
 (0)