Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-02-26-exclusion-M5-14-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`:
- Fix FP reported in #375. Addresses incorrect detection of side effects in unevaluated contexts.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,44 @@ import codingstandards.cpp.autosar
import codingstandards.cpp.SideEffect
import codingstandards.cpp.sideeffect.DefaultEffects

/**
* an operator that does not evaluate its operand
* `decltype` also has a non evaluated operand but cannot be used in a `BinaryLogicalOperation`
*/
class UnevaluatedOperand extends Expr {
Comment thread
knewbury01 marked this conversation as resolved.
Outdated
Expr operator;

UnevaluatedOperand() {
exists(SizeofExprOperator op | op.getExprOperand() = this |
not this.getUnderlyingType().(ArrayType).hasArraySize() and
operator = op
)
or
exists(NoExceptExpr e |
e.getExpr() = this and
operator = e
)
or
exists(TypeidOperator t |
t.getExpr() = this and
operator = t
)
or
exists(FunctionCall declval |
declval.getTarget().hasQualifiedName("std", "declval") and
declval.getAChild() = this and
operator = declval
)
}

Expr getOp() { result = operator }
}

from BinaryLogicalOperation op, Expr rhs
where
not isExcluded(op,
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
rhs = op.getRightOperand() and
hasSideEffect(rhs)
hasSideEffect(rhs) and
not exists(UnevaluatedOperand un | un.getOp() = rhs)
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
10 changes: 10 additions & 0 deletions cpp/autosar/test/rules/M5-14-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ void f3(bool b) {

if (b || f2()) { // COMPLIANT, f2 has local side-effects
}
}

int g1 = 0;
int f4() { return g1++; }
int f5() { return 1; }

void f6() {
if (noexcept(f5()) &&noexcept(
Comment thread
knewbury01 marked this conversation as resolved.
Outdated
f4())) { // COMPLIANT - noexcept operands not evaluated
}
}