Skip to content

Commit adbcd3f

Browse files
committed
CTR55-CPP: address review comments
1 parent fbe1aab commit adbcd3f

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

cpp/cert/src/rules/CTR52-CPP/GuaranteeGenericCppLibraryFunctionsDoNotOverflow.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080
iteratorCreationCall = outputContainer.getAnIteratorFunctionCall() and
8181
iteratorCreationCall = c.getOutputIteratorSource()
8282
|
83-
size_compare_bounds_checked(iteratorCreationCall, c)
83+
sizeCompareBoundsChecked(iteratorCreationCall, c)
8484
or
8585
// Container created with sufficient size for the input
8686
exists(ContainerAccessWithoutRangeCheck::ContainerConstructorCall outputIteratorConstructor |

cpp/cert/src/rules/CTR55-CPP/DoNotUseAnAdditiveOperatorOnAnIterator.ql

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import cpp
1515
import codingstandards.cpp.cert
1616
import codingstandards.cpp.Iterators
17+
import semmle.code.cpp.controlflow.Dominance
1718

1819
/**
1920
* any `.size()` check above our access
2021
*/
21-
predicate size_checked_above(ContainerIteratorAccess it, IteratorSource source) {
22-
exists(STLContainer c, FunctionCall guardCall |
23-
c.getACallToSize() = guardCall and
24-
guardCall = it.getAPredecessor*() and
22+
predicate sizeCheckedAbove(ContainerIteratorAccess it, IteratorSource source) {
23+
exists(ContainerAccessWithoutRangeCheck::ContainerSizeCall guardCall |
24+
strictlyDominates(guardCall, it) and
2525
//make sure its the same container providing its size as giving the iterator
2626
globalValueNumber(guardCall.getQualifier()) = globalValueNumber(source.getQualifier()) and
2727
// and the size call we match must be after the assignment call
@@ -30,16 +30,22 @@ predicate size_checked_above(ContainerIteratorAccess it, IteratorSource source)
3030
}
3131

3232
/**
33-
* some loop check exists like: `iterator != end`
33+
* some guard exists like: `iterator != end`
3434
* where a relevant`.end()` call flowed into end
3535
*/
36-
predicate valid_end_bound_check(ContainerIteratorAccess it, IteratorSource source) {
37-
exists(STLContainer c, Loop l, ContainerIteratorAccess otherAccess, IteratorSource end |
36+
predicate validEndBoundCheck(ContainerIteratorAccess it, IteratorSource source) {
37+
exists(
38+
STLContainer c, BasicBlock b, GuardCondition l, ContainerIteratorAccess otherAccess,
39+
IteratorSource end
40+
|
3841
end = c.getAnIteratorEndFunctionCall() and
39-
//flow exists between end() and the loop condition
40-
DataFlow::localFlow(DataFlow::exprNode(end), DataFlow::exprNode(l.getCondition().getAChild())) and
41-
l.getCondition().getAChild() = otherAccess and
42-
//make sure its the same iterator being checked as incremented
42+
//guard controls the access
43+
l.controls(b, _) and
44+
b.contains(it) and
45+
//guard is comprised of (anything flowing to) end check and an iterator access
46+
DataFlow::localFlow(DataFlow::exprNode(end), DataFlow::exprNode(l.getChild(_))) and
47+
l.getChild(_) = otherAccess and
48+
//make sure its the same iterator being checked in the guard as accessed
4349
otherAccess.getOwningContainer() = it.getOwningContainer() and
4450
//make sure its the same container providing its end as giving the iterator
4551
globalValueNumber(end.getQualifier()) = globalValueNumber(source.getQualifier())
@@ -52,7 +58,7 @@ where
5258
it.isAdditiveOperation() and
5359
not exists(RangeBasedForStmt fs | fs.getUpdate().getAChild*() = it) and
5460
source = it.getANearbyAssigningIteratorCall() and
55-
not size_compare_bounds_checked(source, it) and
56-
not valid_end_bound_check(it, source) and
57-
not size_checked_above(it, source)
61+
not sizeCompareBoundsChecked(source, it) and
62+
not validEndBoundCheck(it, source) and
63+
not sizeCheckedAbove(it, source)
5864
select it, "Increment of iterator may overflow since its bounds are not checked."

cpp/common/src/codingstandards/cpp/Iterators.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ ControlFlowNode getANonInvalidatedSuccessor(ContainerInvalidationOperation op) {
472472
/**
473473
* Guarded by a bounds check that ensures our destination is larger than "some" value
474474
*/
475-
predicate size_compare_bounds_checked(IteratorSource iteratorCreationCall, Expr guarded) {
475+
predicate sizeCompareBoundsChecked(IteratorSource iteratorCreationCall, Expr guarded) {
476476
exists(
477477
GuardCondition guard, ContainerAccessWithoutRangeCheck::ContainerSizeCall sizeCall,
478478
boolean branch

0 commit comments

Comments
 (0)