CPP: Add a test of ComparisonPrecedence.ql.

This commit is contained in:
Geoffrey White
2019-09-24 17:38:53 +01:00
parent 0e79d3db46
commit 4fc73cab63
3 changed files with 78 additions and 0 deletions

View File

@@ -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. |

View File

@@ -0,0 +1 @@
Likely Bugs/Arithmetic/ComparisonPrecedence.ql

View File

@@ -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 `<`)
}
}