Skip to content

M5-14-1: exclusion unevaluated contexts #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -18,11 +18,13 @@ import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.SideEffect
import codingstandards.cpp.sideeffect.DefaultEffects
import codingstandards.cpp.Expr

from BinaryLogicalOperation op, Expr rhs
where
not isExcluded(op,
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
rhs = op.getRightOperand() and
hasSideEffect(rhs)
hasSideEffect(rhs) and
not rhs instanceof UnevaluatedExprExtension
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| test.cpp:15:7:15:14 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:15:12:15:14 | ... ++ | right-hand operand |
| test.cpp:18:7:18:21 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:18:13:18:20 | ... == ... | right-hand operand |
| test.cpp:21:7:21:15 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:21:12:21:13 | call to f1 | right-hand operand |
| test.cpp:40:7:40:41 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:40:26:40:26 | call to operator== | right-hand operand |
16 changes: 16 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,20 @@ void f3(bool b) {

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

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

#include <typeinfo>

void f6() {
if (1 && sizeof(f4())) {
} // COMPLIANT - sizeof operands not evaluated
if (1 &&noexcept(f4()) &&noexcept(f4())) {
} // COMPLIANT - noexcept operands not evaluated

if (1 || (typeid(f5()) == typeid(f4()))) {
} // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is
}
14 changes: 14 additions & 0 deletions cpp/common/src/codingstandards/cpp/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,17 @@ module MisraExpr {
CValue() { isCValue(this) }
}
}

/**
* an operator that does not evaluate its operand
*/
class UnevaluatedExprExtension extends Expr {
UnevaluatedExprExtension() {
this.getAChild().isUnevaluated()
or
exists(FunctionCall declval |
declval.getTarget().hasQualifiedName("std", "declval") and
declval.getAChild() = this
)
}
}
1 change: 1 addition & 0 deletions cpp/common/test/includes/standard-library/typeinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace std {
struct type_info {
const char *name() const noexcept;
std::size_t hash_code() const noexcept;
bool operator==(const type_info &rhs) const;
};
} // namespace std