Skip to content

Commit e4ae957

Browse files
committed
C++: More fixes to overflow detection
1 parent 2606abf commit e4ae957

File tree

4 files changed

+139
-107
lines changed

4 files changed

+139
-107
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/RangeAnalysisStage.qll

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,17 @@ module RangeStage<
941941
semExprDoesntOverflow(upper.booleanNot(), e)
942942
or
943943
not potentiallyOverflowingExpr(upper.booleanNot(), e)
944+
or
945+
initialBounded(e, any(SemZeroBound z), _, upper.booleanNot(), _, _, _)
946+
or
947+
exists(D::Delta otherDelta |
948+
initialBounded(e, _, otherDelta, upper.booleanNot(), _, _, _) and
949+
(
950+
upper = true and D::toFloat(otherDelta) >= 0
951+
or
952+
upper = false and D::toFloat(otherDelta) <= 0
953+
)
954+
)
944955
)
945956
}
946957

@@ -952,16 +963,14 @@ module RangeStage<
952963
(
953964
positively = true and
954965
(
955-
not semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TPos()
956-
or
957-
not semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TPos()
966+
semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TPos() and
967+
semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TPos()
958968
)
959969
or
960970
positively = false and
961971
(
962-
not semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TNeg()
963-
or
964-
not semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TNeg()
972+
semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TNeg() and
973+
semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TNeg()
965974
)
966975
)
967976
or
@@ -972,16 +981,14 @@ module RangeStage<
972981
(
973982
positively = true and
974983
(
975-
not semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TPos()
976-
or
977-
not semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TNeg()
984+
semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TPos() and
985+
semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TNeg()
978986
)
979987
or
980988
positively = false and
981989
(
982-
not semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TNeg()
983-
or
984-
not semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TPos()
990+
semExprSign(expr.(SemBinaryExpr).getLeftOperand()) = TNeg() and
991+
semExprSign(expr.(SemBinaryExpr).getRightOperand()) = TPos()
985992
)
986993
)
987994
or
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import cpp
2+
import experimental.semmle.code.cpp.semantic.analysis.SimpleRangeAnalysis
3+
import TestUtilities.InlineExpectationsTest
4+
5+
class RangeAnalysisTest extends InlineExpectationsTest {
6+
RangeAnalysisTest() { this = "RangeAnalysisTest" }
7+
8+
override string getARelevantTag() { result = "overflow" }
9+
10+
override predicate hasActualResult(Location location, string element, string tag, string value) {
11+
exists(Expr e |
12+
/*call.getArgument(0) = e and
13+
call.getTarget().hasName("range") and*/
14+
tag = "overflow" and
15+
element = e.toString() and
16+
location = e.getLocation() and
17+
value =
18+
strictconcat(string s |
19+
s = "+" and exprMightOverflowPositively(e)
20+
or
21+
s = "-" and exprMightOverflowNegatively(e)
22+
)
23+
)
24+
}
25+
}

0 commit comments

Comments
 (0)