Skip to content

Commit 4fc73ca

Browse files
committed
CPP: Add a test of ComparisonPrecedence.ql.
1 parent 0e79d3d commit 4fc73ca

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| test.cpp:42:6:42:14 | ... < ... | Check the comparison operator precedence. |
2+
| test.cpp:43:6:43:14 | ... > ... | Check the comparison operator precedence. |
3+
| test.cpp:44:6:44:16 | ... <= ... | Check the comparison operator precedence. |
4+
| test.cpp:45:6:45:16 | ... <= ... | Check the comparison operator precedence. |
5+
| test.cpp:46:6:46:14 | ... > ... | Check the comparison operator precedence. |
6+
| test.cpp:50:6:50:32 | ... < ... | Check the comparison operator precedence. |
7+
| test.cpp:51:6:51:18 | ... < ... | Check the comparison operator precedence. |
8+
| test.cpp:54:8:54:16 | ... < ... | Check the comparison operator precedence. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Arithmetic/ComparisonPrecedence.ql
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
/**
3+
* MyClass1 contains an `int` and has well behaved `operator<`
4+
*/
5+
class MyClass1 {
6+
public:
7+
MyClass1() : v(0) {};
8+
MyClass1(int _v) : v(_v) {};
9+
10+
bool operator<(const MyClass1 &other) {
11+
return v < other.v;
12+
}
13+
14+
operator bool() {
15+
return true;
16+
}
17+
18+
int v;
19+
};
20+
21+
/**
22+
* MyClass2 contains an `int` but has an unusual `operator<`
23+
*/
24+
class MyClass2 {
25+
public:
26+
MyClass2() : v(0) {};
27+
MyClass2(int _v) : v(_v) {};
28+
29+
MyClass2 operator<(const MyClass2 &other) {
30+
return MyClass2(other.v);
31+
}
32+
33+
operator bool() {
34+
return true;
35+
}
36+
37+
int v;
38+
};
39+
40+
void test1(int x, int y, int z) {
41+
// built-in comparison
42+
if (x < y < z) {} // BAD
43+
if (x > y > z) {} // BAD
44+
if (x <= y <= z) {} // BAD
45+
if (x <= y <= z) {} // BAD
46+
if (x < y > z) {} // BAD
47+
if ((x < y) && (y < z)) {} // GOOD
48+
if (x < y && y < z) {} // GOOD
49+
50+
if ((x + 1) < (y + 1) < (z + 1)) {} // BAD
51+
if (x < x + y < z) {} // BAD
52+
53+
if ((x < y) < z) {} // GOOD (this is deliberately allowed)
54+
if (!(x < y < z)) {} // BAD
55+
56+
// overloaded comparison
57+
{
58+
MyClass1 a, b, c;
59+
60+
if (a < b < c) {} // BAD (the overloaded `operator<` behaves like `<`) [NOT DETECTED]
61+
}
62+
63+
// overloaded non-comparison
64+
{
65+
MyClass2 a, b, c;
66+
67+
if (a < b < c) {} // GOOD (the overloaded `operator<` does not behave like `<`)
68+
}
69+
}

0 commit comments

Comments
 (0)