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
}

cpp/common/test/includes/standard-library/typeinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace std {
44
struct type_info {
55
const char *name() const noexcept;
66
std::size_t hash_code() const noexcept;
7+
bool operator==(const type_info &rhs) const;
78
};
89
} // namespace std

cpp/common/test/rules/nonbooleanifstmt/test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,16 @@ void test_boolean_conditions() {
4646
if (a) { // COMPLIANT - a has an explicit operator bool()
4747
}
4848
}
49+
50+
template <typename T> bool test_fp_reported_in_10a(T &p1) {
51+
if (p1.length() > 10) { // COMPLIANT
52+
return true;
53+
}
54+
return false;
55+
}
56+
57+
#include <string>
58+
void test_fp_reported_in_10b() {
59+
std::string s;
60+
test_fp_reported_in_10a(s);
61+
}

cpp/common/test/rules/nonbooleaniterationstmt/test.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,17 @@ class ClassC {
4141
if (!d.empty()) { // COMPLIANT
4242
}
4343
}
44-
};
44+
};
45+
46+
#include <vector>
47+
template <typename T> void test_fp_reported_in_10a(std::vector<T> &p1) {
48+
for (typename std::vector<T>::iterator it = p1.begin(); it != p1.end();
49+
++it) { // COMPLIANT
50+
(*it)++;
51+
}
52+
}
53+
54+
void test_fp_reported_in_10b() {
55+
std::vector<int> vl1;
56+
test_fp_reported_in_10a(vl1);
57+
}

docs/development_handbook.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
| 0.29.0 | 2023-10-11 | Remco Vermeulen | Update release process. |
3939
| 0.29.1 | 2023-10-11 | Remco Vermeulen | Address Markdown linter problems. |
4040
| 0.30.0 | 2023-11-14 | Remco Vermeulen | Clarify release steps in case of a hotfix release. |
41+
| 0.31.0 | 2024-02-23 | Remco Vermeulen | Clarify the required use of Python version 3.9 |
4142

4243
## Scope of work
4344

4445
A *coding standard* is a set of rules or guidelines which restrict or prohibit the use of certain dangerous or confusing coding patterns or language features. This repository contains CodeQL queries (and supporting processes) which implement a number of different coding standards. The currently supported standards are:
4546

4647
| Standard | Version | Total rules | Total supportable rules | Status | Notes |
4748
| -------------------------------------------------------------------------------------------------------------------- | ------- | ----------- | ----------------------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
48-
| [AUTOSAR C++](https://www.autosar.org/fileadmin/standards/R22-11/AP/AUTOSAR_RS_CPP14Guidelines.pdf) | R22-11, R21-11, R20-11, R19-11, R19-03 | 397 | 375 | Implemented |
49+
| [AUTOSAR C++](https://www.autosar.org/fileadmin/standards/R22-11/AP/AUTOSAR_RS_CPP14Guidelines.pdf) | R22-11, R21-11, R20-11, R19-11, R19-03 | 397 | 375 | Implemented | |
4950
| [CERT-C++](https://resources.sei.cmu.edu/downloads/secure-coding/assets/sei-cert-cpp-coding-standard-2016-v01.pdf) | 2016 | 83 | 83 | Implemented | AUTOSAR includes a sub-set of rules take from MISRA C++ 2008, which can be purchased for a small fee from [the MISRA website](https://misra.org.uk/shop). |
5051
| [CERT-C](https://resources.sei.cmu.edu/downloads/secure-coding/assets/sei-cert-c-coding-standard-2016-v01.pdf) | 2016 | 120 | 99 | In development | The implementation excludes rules not part of 2016, but that are added to the [CERT-C wiki](https://wiki.sei.cmu.edu/confluence/display/c/) |
5152
| [MISRA C](https://www.misra.org.uk/product/misra-c2012-third-edition-first-revision/ ) | 2012 | 172 | 172 | In development | This includes the [MISRA C:2012 Amendment 2](https://www.misra.org.uk/app/uploads/2021/06/MISRA-C-2012-AMD2.pdf) |
@@ -163,6 +164,7 @@ These files will be ready for query implementation.
163164

164165
#### Step 0: Prepare the Python environment
165166

167+
The tooling standardizes on Python 3.9 and requires the use of version 3.9 to run all tooling.
166168
The `scripts` directory contains the pip package specification file `requirements.txt` that contains the dependencies our generation scripts rely upon.
167169

168170
The dependencies can be installed as follows:
@@ -171,12 +173,12 @@ The dependencies can be installed as follows:
171173
pip3.9 install -r scripts/requirements.txt
172174
```
173175

174-
It is advisable to use a Python virtual environment which needs to be created and activated before installing the dependencies. This can be done as follows:
176+
It is advisable to use a Python 3.9 virtual environment which needs to be created and activated before installing the dependencies. This can be done as follows:
175177

176178
```bash
177179
python3.9 -mvenv scripts/.venv
178180
. scripts/.venv/bin/activate
179-
pip install -r scripts/requirements.txt
181+
pip3.9 install -r scripts/requirements.txt
180182
```
181183

182184
#### Step 1: Generate rule package description file
@@ -265,7 +267,7 @@ The `generate_package_description.py` script provides a "best-effort" approach t
265267
- `performance` - if the query identifies an issue which has a negative impact on the performance of the code.
266268
- `concurrency` - if the query identifies a concurrency issue.
267269
- Validate the rule package description file using the `validate-rule-package.py` script that validates the rule package descriptions against the schema `rule-package.schema.json` located in the `schemas` directory.
268-
- `python3 scripts/validate-rule-package.py <rule_package_name>`
270+
- `python3.9 scripts/validate-rule-package.py <rule_package_name>`
269271

270272
#### Step 3
271273

@@ -350,7 +352,7 @@ All public predicates, classes, modules and files should be documented with QLDo
350352

351353
### Installing QL dependencies
352354

353-
All of our query and library packs depend on the standard CodeQL library for C++, `codeql/cpp-all`. This dependency is specified in the `qlpack.yml` file for each of our packs. Before compiling, running, or testing any of our queries or libraries, you must download the proper dependencies by running `python3 scripts/install-packs.py`. This will download the appropriate version of the standard library from the public package registry, installing it in a cache in your `~/.codeql` directory. When compiling queries or running tests, the QL compiler will pick up the appropriate dependencies from this cache without any need to specify an additional library search path on the command line.
355+
All of our query and library packs depend on the standard CodeQL library for C++, `codeql/cpp-all`. This dependency is specified in the `qlpack.yml` file for each of our packs. Before compiling, running, or testing any of our queries or libraries, you must download the proper dependencies by running `python3.9 scripts/install-packs.py`. This will download the appropriate version of the standard library from the public package registry, installing it in a cache in your `~/.codeql` directory. When compiling queries or running tests, the QL compiler will pick up the appropriate dependencies from this cache without any need to specify an additional library search path on the command line.
354356

355357
Because the downloaded packs are cached, it is only necessary to run `install-packs.py` once each time we upgrade to a new standard library version. It does not hurt to run it more often; if all necessary packs are already in the download cache, then it will complete quickly without trying to download anything.
356358

0 commit comments

Comments
 (0)