Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc25298

Browse files
committed
C++: Demonstrate false positives when a const variable is initialized in a parameter list
1 parent 9fc75f1 commit cc25298

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
void func_with_default_arg(const int n = 0) {
2+
if(n <= 10) {} // GOOD [FALSE POSITIVE]
3+
}
4+
5+
struct A {
6+
const int int_member = 0;
7+
A(int n) : int_member(n) {
8+
if(int_member <= 10) {
9+
10+
}
11+
}
12+
};
13+
14+
struct B {
15+
B(const int n = 0) {
16+
if(n <= 10) {} // GOOD [FALSE POSITIVE]
17+
}
18+
};
19+
20+
const volatile int volatile_const_global = 0;
21+
22+
void test1() {
23+
func_with_default_arg(100);
24+
25+
A a(100);
26+
if(a.int_member <= 10) {}
27+
28+
if(volatile_const_global <= 10) {} // GOOD [FALSE POSITIVE]
29+
}

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
| PointlessComparison.c:303:9:303:14 | ... >= ... | Comparison is always false because c <= 0. |
3939
| PointlessComparison.c:312:9:312:14 | ... >= ... | Comparison is always false because c <= 0. |
4040
| PointlessComparison.c:337:14:337:21 | ... >= ... | Comparison is always true because x >= 0. |
41+
| PointlessComparison.cpp:2:8:2:14 | ... <= ... | Comparison is always true because n <= 0. |
42+
| PointlessComparison.cpp:16:12:16:18 | ... <= ... | Comparison is always true because n <= 0. |
43+
| PointlessComparison.cpp:28:8:28:34 | ... <= ... | Comparison is always true because volatile_const_global <= 0. |
4144
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
4245
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

0 commit comments

Comments
 (0)