File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed
cpp/ql/test/query-tests/Likely Bugs/Arithmetic/ComparisonPrecedence Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change
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. |
Original file line number Diff line number Diff line change
1
+ Likely Bugs/Arithmetic/ComparisonPrecedence.ql
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments