Skip to content

Commit 93f6cdc

Browse files
authored
Merge branch 'main' into lcartey/remove-old-is-excluded
2 parents 797449c + 033dc61 commit 93f6cdc

18 files changed

+222
-127
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ This repository contains CodeQL queries and libraries which support various Codi
66

77
_Carnegie Mellon and CERT are registered trademarks of Carnegie Mellon University._
88

9-
This repository contains CodeQL queries and libraries which support various Coding Standards for the [C++14](https://www.iso.org/standard/64029.html) programming language.
9+
This repository contains CodeQL queries and libraries which support various Coding Standards for the [C++14](https://www.iso.org/standard/64029.html), [C99](https://www.iso.org/standard/29237.html) and [C11](https://www.iso.org/standard/57853.html) programming languages.
1010

1111
The following coding standards are supported:
1212
- [AUTOSAR - Guidelines for the use of C++14 language in critical and safety-related systems (Releases R22-11, R20-11, R19-11 and R19-03)](https://www.autosar.org/fileadmin/standards/R22-11/AP/AUTOSAR_RS_CPP14Guidelines.pdf).
1313
- [MISRA C++:2008](https://www.misra.org.uk) (support limited to the rules specified in AUTOSAR).
1414
- [SEI CERT C++ Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition)](https://resources.sei.cmu.edu/library/asset-view.cfm?assetID=494932)
15-
16-
In addition, the following Coding Standards for the C programming language are under development:
17-
1815
- [SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition)](https://resources.sei.cmu.edu/downloads/secure-coding/assets/sei-cert-c-coding-standard-2016-v01.pdf)
1916
- [MISRA C 2012](https://www.misra.org.uk/product/misra-c2012-third-edition-first-revision/).
2017

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `A5-0-2` - `NonBooleanIterationCondition.ql`:
2+
- Address FP reported in #10. Exclude conditions in uninstantiated templates.
3+
- `M5-3-1` - `EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql`:
4+
- Adjust the alert message to comply with the style guide.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`:
2+
- Fix FP reported in #375. Addresses incorrect detection of side effects in unevaluated contexts.

cpp/autosar/src/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.SideEffect
2020
import codingstandards.cpp.sideeffect.DefaultEffects
21+
import codingstandards.cpp.Expr
2122

2223
from BinaryLogicalOperation op, Expr rhs
2324
where
2425
not isExcluded(op,
2526
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
2627
rhs = op.getRightOperand() and
27-
hasSideEffect(rhs)
28+
hasSideEffect(rhs) and
29+
not rhs instanceof UnevaluatedExprExtension
2830
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"

cpp/autosar/src/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ where
2929
rt = t.getUnderlyingType().getUnspecifiedType() and rt.getBaseType() instanceof BoolType
3030
) and
3131
not operand.isFromUninstantiatedTemplate(_)
32-
select operand, "bool operator called with a non-bool operand of type " + t.getName() + "."
32+
select operand, "Call to bool operator with a non-bool operand of type '" + t.getName() + "'."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
| 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 |
22
| 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 |
33
| 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 |
4+
| 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 |

cpp/autosar/test/rules/M5-14-1/test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@ void f3(bool b) {
2323

2424
if (b || f2()) { // COMPLIANT, f2 has local side-effects
2525
}
26+
}
27+
28+
int g1 = 0;
29+
int f4() { return g1++; }
30+
int f5() { return 1; }
31+
32+
#include <typeinfo>
33+
34+
void f6() {
35+
if (1 && sizeof(f4())) {
36+
} // COMPLIANT - sizeof operands not evaluated
37+
if (1 &&noexcept(f4()) &&noexcept(f4())) {
38+
} // COMPLIANT - noexcept operands not evaluated
39+
40+
if (1 || (typeid(f5()) == typeid(f4()))) {
41+
} // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is
2642
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| test.cpp:10:8:10:8 | 0 | bool operator called with a non-bool operand of type int. |
2-
| test.cpp:12:7:12:7 | 0 | bool operator called with a non-bool operand of type int. |
3-
| test.cpp:12:13:12:17 | ... + ... | bool operator called with a non-bool operand of type int. |
1+
| test.cpp:10:8:10:8 | 0 | Call to bool operator with a non-bool operand of type 'int'. |
2+
| test.cpp:12:7:12:7 | 0 | Call to bool operator with a non-bool operand of type 'int'. |
3+
| test.cpp:12:13:12:17 | ... + ... | Call to bool operator with a non-bool operand of type 'int'. |

cpp/common/src/codingstandards/cpp/Expr.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,17 @@ module MisraExpr {
189189
CValue() { isCValue(this) }
190190
}
191191
}
192+
193+
/**
194+
* an operator that does not evaluate its operand
195+
*/
196+
class UnevaluatedExprExtension extends Expr {
197+
UnevaluatedExprExtension() {
198+
this.getAChild().isUnevaluated()
199+
or
200+
exists(FunctionCall declval |
201+
declval.getTarget().hasQualifiedName("std", "declval") and
202+
declval.getAChild() = this
203+
)
204+
}
205+
}

cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Provides a library which includes a `problems` predicate for reporting....
2+
* Provides a library which includes a `problems` predicate for reporting non-boolean iteration conditions.
33
*/
44

55
import cpp
@@ -16,8 +16,10 @@ query predicate problems(Loop loopStmt, string message) {
1616
condition = loopStmt.getCondition() and
1717
explicitConversionType = condition.getExplicitlyConverted().getType().getUnspecifiedType() and
1818
not explicitConversionType instanceof BoolType and
19-
//exclude any generated conditions
19+
// exclude any generated conditions
2020
not condition.isCompilerGenerated() and
21+
// exclude any conditions in uninstantiated templates, because their type will be unknown.
22+
not condition.isFromUninstantiatedTemplate(_) and
2123
message = "Iteration condition has non boolean type " + explicitConversionType + "."
2224
)
2325
}

0 commit comments

Comments
 (0)