Skip to content

Commit 6a997ab

Browse files
committed
C++: fix equality refinement in new range analysis
1 parent 383b2e1 commit 6a997ab

File tree

2 files changed

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

2 files changed

+34
-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;
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];
103+
continue;
104+
}
105+
106+
if (i == MAX_SIZE || n != arr[i]) {
107+
if (i == MAX_SIZE) {
108+
break;
109+
}
110+
n = arr[i];
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)