mirror of
https://github.com/github/codeql.git
synced 2026-04-28 18:25:24 +02:00
CPP: Add a test of ComparisonPrecedence.ql.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
| test.cpp:42:6:42:14 | ... < ... | Check the comparison operator precedence. |
|
||||
| test.cpp:43:6:43:14 | ... > ... | Check the comparison operator precedence. |
|
||||
| test.cpp:44:6:44:16 | ... <= ... | Check the comparison operator precedence. |
|
||||
| test.cpp:45:6:45:16 | ... <= ... | Check the comparison operator precedence. |
|
||||
| test.cpp:46:6:46:14 | ... > ... | Check the comparison operator precedence. |
|
||||
| test.cpp:50:6:50:32 | ... < ... | Check the comparison operator precedence. |
|
||||
| test.cpp:51:6:51:18 | ... < ... | Check the comparison operator precedence. |
|
||||
| test.cpp:54:8:54:16 | ... < ... | Check the comparison operator precedence. |
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Arithmetic/ComparisonPrecedence.ql
|
||||
@@ -0,0 +1,69 @@
|
||||
|
||||
/**
|
||||
* MyClass1 contains an `int` and has well behaved `operator<`
|
||||
*/
|
||||
class MyClass1 {
|
||||
public:
|
||||
MyClass1() : v(0) {};
|
||||
MyClass1(int _v) : v(_v) {};
|
||||
|
||||
bool operator<(const MyClass1 &other) {
|
||||
return v < other.v;
|
||||
}
|
||||
|
||||
operator bool() {
|
||||
return true;
|
||||
}
|
||||
|
||||
int v;
|
||||
};
|
||||
|
||||
/**
|
||||
* MyClass2 contains an `int` but has an unusual `operator<`
|
||||
*/
|
||||
class MyClass2 {
|
||||
public:
|
||||
MyClass2() : v(0) {};
|
||||
MyClass2(int _v) : v(_v) {};
|
||||
|
||||
MyClass2 operator<(const MyClass2 &other) {
|
||||
return MyClass2(other.v);
|
||||
}
|
||||
|
||||
operator bool() {
|
||||
return true;
|
||||
}
|
||||
|
||||
int v;
|
||||
};
|
||||
|
||||
void test1(int x, int y, int z) {
|
||||
// built-in comparison
|
||||
if (x < y < z) {} // BAD
|
||||
if (x > y > z) {} // BAD
|
||||
if (x <= y <= z) {} // BAD
|
||||
if (x <= y <= z) {} // BAD
|
||||
if (x < y > z) {} // BAD
|
||||
if ((x < y) && (y < z)) {} // GOOD
|
||||
if (x < y && y < z) {} // GOOD
|
||||
|
||||
if ((x + 1) < (y + 1) < (z + 1)) {} // BAD
|
||||
if (x < x + y < z) {} // BAD
|
||||
|
||||
if ((x < y) < z) {} // GOOD (this is deliberately allowed)
|
||||
if (!(x < y < z)) {} // BAD
|
||||
|
||||
// overloaded comparison
|
||||
{
|
||||
MyClass1 a, b, c;
|
||||
|
||||
if (a < b < c) {} // BAD (the overloaded `operator<` behaves like `<`) [NOT DETECTED]
|
||||
}
|
||||
|
||||
// overloaded non-comparison
|
||||
{
|
||||
MyClass2 a, b, c;
|
||||
|
||||
if (a < b < c) {} // GOOD (the overloaded `operator<` does not behave like `<`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user