Skip to content

Commit f9a4646

Browse files
authored
Merge pull request github#13226 from rdmarsh2/rdmarsh2/cpp/cobo-neq-refinement
C++: fix equality refinement in new range analysis
2 parents fca5fb6 + 7404bd9 commit f9a4646

File tree

3 files changed

+45
-1
lines changed
  • cpp/ql
    • lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis
    • test
      • experimental/query-tests/Security/CWE/CWE-193/constant-size
      • library-tests/ir/range-analysis

3 files changed

+45
-1
lines changed

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisStage.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ module RangeStage<
729729
) {
730730
exists(SemExpr e, D::Delta d1, D::Delta d2 |
731731
unequalFlowStepIntegralSsa(v, pos, e, d1, reason) and
732-
boundedUpper(e, b, d1) and
732+
boundedUpper(e, b, d2) and
733733
boundedLower(e, b, d2) and
734734
delta = D::fromFloat(D::toFloat(d1) + D::toFloat(d2))
735735
)

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,36 @@ void testInterproc(BigArray *arr) {
7878

7979
addToPointerAndAssign(arr->buf);
8080
}
81+
82+
void testEqRefinement() {
83+
int arr[MAX_SIZE];
84+
85+
for(int i = 0; i <= MAX_SIZE; i++) {
86+
if(i != MAX_SIZE) {
87+
arr[i] = 0; // GOOD
88+
}
89+
}
90+
}
91+
92+
void testEqRefinement2() {
93+
int arr[MAX_SIZE];
94+
95+
int n = 0;
96+
97+
for(int i = 0; i <= MAX_SIZE; i++) {
98+
if(n == 0) {
99+
if(i == MAX_SIZE) {
100+
break;
101+
}
102+
n = arr[i]; // GOOD
103+
continue;
104+
}
105+
106+
if (i == MAX_SIZE || n != arr[i]) {
107+
if (i == MAX_SIZE) {
108+
break;
109+
}
110+
n = arr[i]; // GOOD
111+
}
112+
}
113+
}

cpp/ql/test/library-tests/ir/range-analysis/test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,14 @@
5959
range(i); // $ range=>=0 SPURIOUS: range="<=call to f3_get-1" range="<=call to f3_get-2"
6060
}
6161
}
62+
63+
int f4(int x) {
64+
for (int i = 0; i <= 100; i++) {
65+
range(i); // $ range=<=100 range=>=0
66+
if(i == 100) {
67+
range(i); // $ range===100
68+
} else {
69+
range(i); // $ range=<=99 range=>=0
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)