Skip to content

Commit ac3421f

Browse files
authored
Merge pull request #1238 from geoffw0/newtests
CPP: New test cases
2 parents d3f6099 + 3ceacff commit ac3421f

File tree

9 files changed

+123
-0
lines changed

9 files changed

+123
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:7:3:7:24 | call to MyRect | The constructor MyRect may leave the instance uninitialized, as it tries to delegate to another constructor. |
2+
| test.cpp:16:3:16:24 | call to MyRect | The constructor MyRect may leave the instance uninitialized, as it tries to delegate to another constructor. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/OO/IncorrectConstructorDelegation.ql
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
class MyRect
3+
{
4+
public:
5+
MyRect()
6+
{
7+
MyRect(100.0f, 100.0f); // BAD
8+
}
9+
10+
MyRect(float _width, float _height) : width(_width), height(_height)
11+
{
12+
}
13+
14+
MyRect(float _width)
15+
{
16+
MyRect(_width, _width); // BAD
17+
}
18+
19+
MyRect(int a) : MyRect(10.0f, 10.0f) // GOOD
20+
{
21+
MyRect other1(20.0f, 20.0f); // GOOD
22+
MyRect other2 = MyRect(30.0f, 30.0f); // GOOD
23+
}
24+
25+
MyRect(int a, int b)
26+
{
27+
*this = MyRect(40.0f, 40.0f); // GOOD
28+
}
29+
30+
private:
31+
float width, height;
32+
};

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/OverflowBuffer.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
| tests.cpp:519:3:519:8 | call to memset | This 'memset' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:510:16:510:21 | call to malloc | destination buffer |
5757
| tests.cpp:541:6:541:10 | call to fread | This 'fread' operation may access 101 bytes but the $@ is only 100 bytes. | tests.cpp:532:7:532:16 | charBuffer | destination buffer |
5858
| tests.cpp:546:6:546:10 | call to fread | This 'fread' operation may access 400 bytes but the $@ is only 100 bytes. | tests.cpp:532:7:532:16 | charBuffer | destination buffer |
59+
| tests.cpp:569:6:569:15 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:565:7:565:12 | buffer | array |
60+
| tests.cpp:577:7:577:13 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:565:7:565:12 | buffer | array |
61+
| tests.cpp:577:7:577:13 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:571:8:571:13 | buffer | array |
62+
| tests.cpp:579:6:579:12 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:565:7:565:12 | buffer | array |
63+
| tests.cpp:579:6:579:12 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:571:8:571:13 | buffer | array |
5964
| tests_restrict.c:12:2:12:7 | call to memcpy | This 'memcpy' operation accesses 2 bytes but the $@ is only 1 byte. | tests_restrict.c:7:6:7:13 | smallbuf | source buffer |
6065
| unions.cpp:26:2:26:7 | call to memset | This 'memset' operation accesses 200 bytes but the $@ is only 100 bytes. | unions.cpp:21:10:21:11 | mu | destination buffer |
6166
| unions.cpp:30:2:30:7 | call to memset | This 'memset' operation accesses 200 bytes but the $@ is only 100 bytes. | unions.cpp:15:7:15:11 | small | destination buffer |

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/tests.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,32 @@ void test20()
560560
}
561561
}
562562

563+
void test21(bool cond)
564+
{
565+
char buffer[100];
566+
char *ptr;
567+
int i;
568+
569+
if (buffer[-1] == 0) { return; } // BAD: accesses buffer[-1]
570+
571+
ptr = buffer;
572+
if (cond)
573+
{
574+
ptr++;
575+
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[0]
576+
} else {
577+
if (ptr[-1] == 0) { return; } // BAD: accesses buffer[-1]
578+
}
579+
if (ptr[-1] == 0) { return; } // BAD: accesses buffer[-1] or buffer[0]
580+
581+
ptr = buffer;
582+
for (i = 0; i < 2; i++)
583+
{
584+
ptr++;
585+
}
586+
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[1]
587+
}
588+
563589
int main(int argc, char *argv[])
564590
{
565591
long long arr17[19];
@@ -582,6 +608,7 @@ int main(int argc, char *argv[])
582608
test18();
583609
test19(argc == 0);
584610
test20();
611+
test21(argc == 0);
585612

586613
return 0;
587614
}

cpp/ql/test/query-tests/Security/CWE/CWE-772/semmle/tests-memory/test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,20 @@ void test28()
555555
dostuff();
556556
}
557557

558+
// placement new
559+
void* operator new(size_t, void* p);
560+
561+
class MyClass29
562+
{
563+
};
564+
565+
void test29()
566+
{
567+
void *buf = malloc(sizeof(MyClass29)); // GOOD (freed)
568+
MyClass29 *p1 = new (buf) MyClass29(); // GOOD (not really an allocation)
569+
free(buf);
570+
}
571+
558572
// run tests
559573
int main(int argc, char *argv[])
560574
{
@@ -585,4 +599,5 @@ int main(int argc, char *argv[])
585599
test26();
586600
test27();
587601
test28();
602+
test29();
588603
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:14:6:14:15 | not_called | AV Rule 186: There shall be no unreachable code. |
2+
| test.c:32:3:32:6 | ExprStmt | AV Rule 186: There shall be no unreachable code. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jsf/4.24 Control Flow Structures/AV Rule 186.ql
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
int x = 0;
3+
4+
void called1()
5+
{
6+
x++;
7+
}
8+
9+
void called2()
10+
{
11+
x++;
12+
}
13+
14+
void not_called()
15+
{
16+
x++; // BAD: unreachable
17+
}
18+
19+
int main(int argc, const char* argv[])
20+
{
21+
void (*fun_ptr)() = &called2;
22+
23+
called1();
24+
called2();
25+
26+
if (argc > 4)
27+
{
28+
x++;
29+
while (1) {
30+
x++;
31+
}
32+
x++; // BAD: unreachable
33+
} else if (argc > 4) {
34+
x++; // BAD: unreachable [NOT DETECTED]
35+
} else if (argc > 5) {
36+
x++; // BAD: unreachable [NOT DETECTED]
37+
}
38+
}

0 commit comments

Comments
 (0)