Skip to content

Commit 7e49686

Browse files
committed
A18-1-4: Fix compiler compatibility issues with reset
reset() is sometimes declared on a base class. Similar issue to A8-4-13, so I have refactored the SmartPointer class to provide predicates which identify the operations across multiple compilers.
1 parent 25bc94d commit 7e49686

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

cpp/autosar/src/rules/A18-1-4/PointerToAnElementOfAnArrayPassedToASmartPointer.ql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ class SingleObjectSmartPointerArrayConstructionConfig extends TaintTracking::Con
4646
(
4747
sp.getAConstructorCallWithExternalObjectConstruction().getAnArgument() = sink.asExpr()
4848
or
49-
sink.asExpr() =
50-
any(FunctionCall fc, MemberFunction mf |
51-
mf = fc.getTarget() and
52-
mf.getDeclaringType() = sp and
53-
mf.getName() = "reset"
54-
|
55-
fc.getArgument(0)
56-
)
49+
sink.asExpr() = sp.getAResetCall().getArgument(0)
5750
)
5851
)
5952
}

cpp/autosar/src/rules/A8-4-13/SharedPtrPassedToFunctionWithImproperSemantics.ql

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,20 @@ import cpp
1919
import codingstandards.cpp.autosar
2020
import codingstandards.cpp.SmartPointers
2121

22-
class AutosarSharedPointerOrDerived extends Type {
23-
AutosarSharedPointerOrDerived() {
24-
this.getUnspecifiedType() instanceof AutosarSharedPointer or
25-
this.getUnspecifiedType().(DerivedType).getBaseType() instanceof AutosarSharedPointer
26-
}
27-
}
28-
29-
Expr underlyingObjectAffectingSharedPointerExpr(Function f) {
30-
result =
31-
any(VariableAccess va, FunctionCall fc |
32-
va.getEnclosingFunction() = f and
33-
// The type of the variable is either a shared_ptr, or a reference or pointer to a shared_ptr
34-
va.getType() instanceof AutosarSharedPointerOrDerived and
35-
fc.getQualifier() = va and
36-
// include only calls to methods which modify the underlying object
37-
fc.getTarget().hasName(["operator=", "reset", "swap"])
38-
|
39-
va
40-
)
22+
VariableAccess underlyingObjectAffectingSharedPointerExpr(Function f) {
23+
exists(FunctionCall fc |
24+
// Find a call in the function
25+
fc.getEnclosingFunction() = f and
26+
// include only calls to methods which modify the underlying object
27+
fc = any(AutosarSharedPointer s).getAModifyingCall() and
28+
// Report the qualifier
29+
fc.getQualifier() = result
30+
)
4131
}
4232

4333
predicate flowsToUnderlyingObjectAffectingExpr(Parameter p) {
4434
// check if a parameter flows locally to an expression which affects smart pointer lifetime
45-
p.getType() instanceof AutosarSharedPointerOrDerived and
35+
p.getType().stripType() instanceof AutosarSharedPointer and
4636
localExprFlow(p.getAnAccess(), underlyingObjectAffectingSharedPointerExpr(p.getFunction()))
4737
or
4838
// else handle nested cases, such as passing smart pointers as reference arguments
@@ -60,7 +50,7 @@ predicate flowsToUnderlyingObjectAffectingExpr(Parameter p) {
6050
from DefinedSmartPointerParameter p, string problem
6151
where
6252
not isExcluded(p, SmartPointers1Package::smartPointerAsParameterWithoutLifetimeSemanticsQuery()) and
63-
p.getType() instanceof AutosarSharedPointerOrDerived and
53+
p.getType().stripType() instanceof AutosarSharedPointer and
6454
(
6555
// handle the parameter depending on its derived type
6656
p.getType() instanceof RValueReferenceType and

cpp/common/src/codingstandards/cpp/SmartPointers.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ abstract class AutosarSmartPointer extends Class {
5151
AutosarSmartPointer
5252
)
5353
}
54+
55+
FunctionCall getAResetCall() {
56+
result.getTarget().hasName("reset") and
57+
result.getQualifier().getType().stripType() = this
58+
}
59+
60+
FunctionCall getAModifyingCall() {
61+
result.getTarget().hasName(["operator=", "reset", "swap"]) and
62+
result.getQualifier().getType().stripType() = this
63+
}
5464
}
5565

5666
class AutosarUniquePointer extends AutosarSmartPointer {

0 commit comments

Comments
 (0)