Files
codeql/cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/RegressionTests.cpp
2026-01-06 13:53:43 +01:00

167 lines
2.8 KiB
C++

// Regression test. The AsmStmt might modify the arguments, which are passed by
// reference.
// Helper function for regression_test_00, below.
static void modify_args(unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d)
{
#if defined(__GNUC__)
__asm__ __volatile__
(
"cpuid\n\t"
: "+a" (a), "+b" (b), "+c" (c), "+d" (d)
);
#else
a++;
b++;
c++;
d++;
#endif
}
int regression_test_00() {
unsigned int a = 0, b = 0, c = 0, d = 0;
modify_args(a, b, c, d);
const unsigned int x = a;
// 'a' might have been modified by the call to 'modify_args',
// so we do not know if this condition is true or false.
if (x >= 1)
{
return true;
}
return false;
}
static const unsigned int e = -1;
void test_e(int f) {
if (f == e) { // GOOD
// ...
}
}
#define MAX_VAL ((size_t) -1)
typedef unsigned int size_t;
static int foo(size_t *size)
{
int bar;
if (*size <= MAX_VAL) // BAD (pointless comparison) [NO LONGER REPORTED]
*size = MAX_VAL;
}
// ODASA-7205
int regression_test_01(unsigned long bb) {
if (bb + 1 == 0) { // GOOD [NO LONGER REPORTED]
return 0;
} else {
return 1;
}
}
int containsIfDef(int x) {
int result = 0;
if (x > 0) {
result = 1;
}
#if _CONDITION
if (x < 0) {
result = -1;
}
#endif
return result >= 0;
}
void negativeZero1(int val) {
if (val >= 0)
{
val = -val;
}
if (val == 0) // GOOD [NO LONGER REPORTED]
;
}
void negativeZero2(int val) {
if (val >= 0)
{
val = 0 - val;
}
if (val == 0) // GOOD
;
}
void negativeZero3(int val) {
if (val >= 0)
{
val *= -1;
}
if (val == 0) // GOOD [NO LONGER REPORTED]
;
}
void negativeZero4(int val) {
if (val >= 0)
{
val = val * -1;
}
if (val == 0) // GOOD [NO LONGER REPORTED]
;
}
void f(int *const &ref_to_ptr);
void testTempObject() {
int x = 0;
f(&x);
if (x > 0) {} // GOOD [NO LONGER REPORTED]
}
void staticAssert() {
static const int a = 42;
static const int b = 43;
static_assert(a < b + 0, ""); // GOOD
}
constexpr int global_1 = 42;
constexpr int global_2 = global_1 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD
static const int global_3 = 42;
static const int global_4 = global_3 < 2 * sizeof(int*) ? 43 : 2 * sizeof(int*); // GOOD
template<unsigned int p, unsigned int n, bool = ((2u * n) < p)>
struct templateCompare : public templateCompare<p, 2u * n> // GOOD
{ };
template< unsigned int p, unsigned int n>
struct templateCompare< p, n, false>
{
static const unsigned int v = n;
};
unsigned int templateCompare_x = templateCompare<42, 42>::v;
template<int n>
struct someType {
typedef someType<((n - 4) < 0 ? 0 : n - 4)> b; // GOOD
};
someType<42>::b someType_x;
struct A_Struct {
int x;
int y;
};
enum E {
E_e = sizeof(A_Struct) * 8 > 50 // GOOD [FALSE POSITIVE]
};