Skip to content

Commit 06b36f7

Browse files
authored
Merge pull request github#6745 from andersfugmann/handle_overflow_for_upperbound
C++: Handle overflow for upperbound
2 parents 8debae1 + aebde18 commit 06b36f7

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,8 @@ private float getGuardedUpperBound(VariableAccess guardedAccess) {
15491549
// that there is one predecessor, albeit somewhat conservative.
15501550
exists(unique(BasicBlock b | b = def.(BasicBlock).getAPredecessor())) and
15511551
guardedAccess = def.getAUse(v) and
1552-
result = max(float ub | upperBoundFromGuard(guard, guardVa, ub, branch))
1552+
result = max(float ub | upperBoundFromGuard(guard, guardVa, ub, branch)) and
1553+
not convertedExprMightOverflow(guard.getAChild+())
15531554
)
15541555
}
15551556

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/lowerBound.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,10 @@
599599
| test.c:675:7:675:7 | y | -2147483648 |
600600
| test.c:684:7:684:7 | x | -2147483648 |
601601
| test.c:689:7:689:7 | x | -2147483648 |
602+
| test.c:696:8:696:8 | x | 2147483647 |
603+
| test.c:696:12:696:12 | y | 256 |
604+
| test.c:697:9:697:9 | x | 2147483647 |
605+
| test.c:698:9:698:9 | y | 256 |
602606
| test.cpp:10:7:10:7 | b | -2147483648 |
603607
| test.cpp:11:5:11:5 | x | -2147483648 |
604608
| test.cpp:13:10:13:10 | x | -2147483648 |

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,12 @@ void test(int x) {
689689
out(x);
690690
goto label;
691691
}
692+
693+
void test_overflow() {
694+
const int x = 2147483647; // 2^31-1
695+
const int y = 256;
696+
if ((x + y) <= 512) {
697+
out(x);
698+
out(y);
699+
}
700+
}

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,10 @@
599599
| test.c:675:7:675:7 | y | 2147483647 |
600600
| test.c:684:7:684:7 | x | 2147483647 |
601601
| test.c:689:7:689:7 | x | 15 |
602+
| test.c:696:8:696:8 | x | 2147483647 |
603+
| test.c:696:12:696:12 | y | 256 |
604+
| test.c:697:9:697:9 | x | 2147483647 |
605+
| test.c:698:9:698:9 | y | 256 |
602606
| test.cpp:10:7:10:7 | b | 2147483647 |
603607
| test.cpp:11:5:11:5 | x | 2147483647 |
604608
| test.cpp:13:10:13:10 | x | 2147483647 |

0 commit comments

Comments
 (0)