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

Skip to content

Commit e1942bb

Browse files
committed
C++: Fix false positives
1 parent cc25298 commit e1942bb

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

cpp/ql/src/semmle/code/cpp/rangeanalysis/SimpleRangeAnalysis.qll

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,20 @@ private string getValue(Expr e) {
101101
then result = e.getValue()
102102
else
103103
exists(VariableAccess access, Variable v |
104+
/*
105+
* It should be safe to propagate the initialization value to a variable if:
106+
* The type of v is const, and
107+
* The type of v is not volatile, and
108+
* Either:
109+
* v is a local/global variable, or
110+
* v is a static member variable
111+
*/
112+
113+
(v instanceof StaticStorageDurationVariable or v instanceof LocalVariable) and
114+
not v.getUnderlyingType().isVolatile() and
115+
v.getUnderlyingType().isConst() and
104116
e = access and
105117
v = access.getTarget() and
106-
v.getUnderlyingType().isConst() and
107118
result = getValue(v.getAnAssignedValue())
108119
)
109120
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
void func_with_default_arg(const int n = 0) {
2-
if(n <= 10) {} // GOOD [FALSE POSITIVE]
2+
if(n <= 10) {}
33
}
44

55
struct A {
@@ -13,7 +13,7 @@ struct A {
1313

1414
struct B {
1515
B(const int n = 0) {
16-
if(n <= 10) {} // GOOD [FALSE POSITIVE]
16+
if(n <= 10) {}
1717
}
1818
};
1919

@@ -25,5 +25,5 @@ void test1() {
2525
A a(100);
2626
if(a.int_member <= 10) {}
2727

28-
if(volatile_const_global <= 10) {} // GOOD [FALSE POSITIVE]
28+
if(volatile_const_global <= 10) {}
2929
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,5 @@
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. |
4441
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
4542
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

0 commit comments

Comments
 (0)