File tree Expand file tree Collapse file tree
src/semmle/code/cpp/rangeanalysis
test/query-tests/Likely Bugs/Arithmetic/PointlessComparison Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11void func_with_default_arg (const int n = 0 ) {
2- if (n <= 10 ) {} // GOOD [FALSE POSITIVE]
2+ if (n <= 10 ) {}
33}
44
55struct A {
@@ -13,7 +13,7 @@ struct A {
1313
1414struct 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}
Original file line number Diff line number Diff line change 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. |
You can’t perform that action at this time.
0 commit comments