C++: Fix false positives

This commit is contained in:
Mathias Vorreiter Pedersen
2020-03-13 17:02:35 +01:00
parent cc25298f67
commit e1942bbee1
3 changed files with 15 additions and 7 deletions

View File

@@ -101,9 +101,20 @@ private string getValue(Expr e) {
then result = e.getValue()
else
exists(VariableAccess access, Variable v |
/*
* It should be safe to propagate the initialization value to a variable if:
* The type of v is const, and
* The type of v is not volatile, and
* Either:
* v is a local/global variable, or
* v is a static member variable
*/
(v instanceof StaticStorageDurationVariable or v instanceof LocalVariable) and
not v.getUnderlyingType().isVolatile() and
v.getUnderlyingType().isConst() and
e = access and
v = access.getTarget() and
v.getUnderlyingType().isConst() and
result = getValue(v.getAnAssignedValue())
)
}

View File

@@ -1,5 +1,5 @@
void func_with_default_arg(const int n = 0) {
if(n <= 10) {} // GOOD [FALSE POSITIVE]
if(n <= 10) {}
}
struct A {
@@ -13,7 +13,7 @@ struct A {
struct B {
B(const int n = 0) {
if(n <= 10) {} // GOOD [FALSE POSITIVE]
if(n <= 10) {}
}
};
@@ -25,5 +25,5 @@ void test1() {
A a(100);
if(a.int_member <= 10) {}
if(volatile_const_global <= 10) {} // GOOD [FALSE POSITIVE]
if(volatile_const_global <= 10) {}
}

View File

@@ -38,8 +38,5 @@
| PointlessComparison.c:303:9:303:14 | ... >= ... | Comparison is always false because c <= 0. |
| PointlessComparison.c:312:9:312:14 | ... >= ... | Comparison is always false because c <= 0. |
| PointlessComparison.c:337:14:337:21 | ... >= ... | Comparison is always true because x >= 0. |
| PointlessComparison.cpp:2:8:2:14 | ... <= ... | Comparison is always true because n <= 0. |
| PointlessComparison.cpp:16:12:16:18 | ... <= ... | Comparison is always true because n <= 0. |
| PointlessComparison.cpp:28:8:28:34 | ... <= ... | Comparison is always true because volatile_const_global <= 0. |
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |