|
14 | 14 | import cpp
|
15 | 15 | import codingstandards.cpp.cert
|
16 | 16 | import codingstandards.cpp.Iterators
|
| 17 | +import semmle.code.cpp.controlflow.Dominance |
17 | 18 |
|
18 |
| -from ContainerIteratorAccess it |
| 19 | +/** |
| 20 | + * Models a call to an iterator's `operator+` |
| 21 | + */ |
| 22 | +class AdditionOperatorFunctionCall extends AdditiveOperatorFunctionCall { |
| 23 | + AdditionOperatorFunctionCall() { this.getTarget().hasName("operator+") } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * There exists a calculation for the reference one passed the end of some container |
| 28 | + * An example derivation is: |
| 29 | + * `end = begin() + size()` |
| 30 | + */ |
| 31 | +Expr getDerivedReferenceToOnePassedTheEndElement(Expr containerReference) { |
| 32 | + exists( |
| 33 | + ContainerAccessWithoutRangeCheck::ContainerSizeCall size, |
| 34 | + ContainerAccessWithoutRangeCheck::ContainerBeginCall begin, AdditionOperatorFunctionCall calc |
| 35 | + | |
| 36 | + result = calc |
| 37 | + | |
| 38 | + DataFlow::localFlow(DataFlow::exprNode(size), DataFlow::exprNode(calc.getAChild+())) and |
| 39 | + DataFlow::localFlow(DataFlow::exprNode(begin), DataFlow::exprNode(calc.getAChild+())) and |
| 40 | + //make sure its the same container providing its size as giving the begin |
| 41 | + globalValueNumber(begin.getQualifier()) = globalValueNumber(size.getQualifier()) and |
| 42 | + containerReference = begin.getQualifier() |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * a wrapper predicate for a couple of types of permitted end bounds checks |
| 48 | + */ |
| 49 | +Expr getReferenceToOnePassedTheEndElement(Expr containerReference) { |
| 50 | + //a container end access - v.end() |
| 51 | + result instanceof ContainerAccessWithoutRangeCheck::ContainerEndCall and |
| 52 | + containerReference = result.(FunctionCall).getQualifier() |
| 53 | + or |
| 54 | + result = getDerivedReferenceToOnePassedTheEndElement(containerReference) |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * some guard exists like: `iterator != end` |
| 59 | + * where a relevant`.end()` call flowed into end |
| 60 | + */ |
| 61 | +predicate isUpperBoundEndCheckedIteratorAccess(IteratorSource source, ContainerIteratorAccess it) { |
| 62 | + exists( |
| 63 | + Expr referenceToOnePassedTheEndElement, BasicBlock basicBlockOfIteratorAccess, |
| 64 | + GuardCondition upperBoundCheck, ContainerIteratorAccess checkedIteratorAccess, |
| 65 | + Expr containerReferenceFromEndGuard |
| 66 | + | |
| 67 | + //sufficient end guard |
| 68 | + referenceToOnePassedTheEndElement = |
| 69 | + getReferenceToOnePassedTheEndElement(containerReferenceFromEndGuard) and |
| 70 | + //guard controls the access |
| 71 | + upperBoundCheck.controls(basicBlockOfIteratorAccess, _) and |
| 72 | + basicBlockOfIteratorAccess.contains(it) and |
| 73 | + //guard is comprised of end check and an iterator access |
| 74 | + DataFlow::localFlow(DataFlow::exprNode(referenceToOnePassedTheEndElement), |
| 75 | + DataFlow::exprNode(upperBoundCheck.getChild(_))) and |
| 76 | + upperBoundCheck.getChild(_) = checkedIteratorAccess and |
| 77 | + //make sure its the same iterator being checked in the guard as accessed |
| 78 | + checkedIteratorAccess.getOwningContainer() = it.getOwningContainer() and |
| 79 | + //if its the end call itself (or its parts), make sure its the same container providing its end as giving the iterator |
| 80 | + globalValueNumber(containerReferenceFromEndGuard) = globalValueNumber(source.getQualifier()) and |
| 81 | + // and the guard call we match must be after the assignment call (to avoid valid guards protecting new iterator accesses further down) |
| 82 | + source.getASuccessor*() = upperBoundCheck |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +from ContainerIteratorAccess it, IteratorSource source |
19 | 87 | where
|
20 | 88 | not isExcluded(it, IteratorsPackage::doNotUseAnAdditiveOperatorOnAnIteratorQuery()) and
|
21 | 89 | it.isAdditiveOperation() and
|
22 | 90 | not exists(RangeBasedForStmt fs | fs.getUpdate().getAChild*() = it) and
|
23 |
| - // we get the neraby assignment |
24 |
| - not exists(STLContainer c, FunctionCall nearbyAssigningIteratorCall, FunctionCall guardCall | |
25 |
| - nearbyAssigningIteratorCall = it.getANearbyAssigningIteratorCall() and |
26 |
| - // we look for calls to size or end |
27 |
| - (guardCall = c.getACallToSize() or guardCall = c.getAnIteratorEndFunctionCall()) and |
28 |
| - // such that the call to size is before this |
29 |
| - // access |
30 |
| - guardCall = it.getAPredecessor*() and |
31 |
| - // and it uses the same qualifier as the one we were just assigned |
32 |
| - nearbyAssigningIteratorCall.getQualifier().(VariableAccess).getTarget() = |
33 |
| - guardCall.getQualifier().(VariableAccess).getTarget() and |
34 |
| - // and the size call we match must be after the assignment call |
35 |
| - nearbyAssigningIteratorCall.getASuccessor*() = guardCall |
36 |
| - ) |
| 91 | + source = it.getANearbyAssigningIteratorCall() and |
| 92 | + not isUpperBoundEndCheckedIteratorAccess(source, it) and |
| 93 | + not sizeCompareBoundsChecked(source, it) |
37 | 94 | select it, "Increment of iterator may overflow since its bounds are not checked."
|
0 commit comments