From 819e9f9cc299165b1b11fa88b17f4f323318a31e Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 22 Feb 2024 15:09:35 -0800 Subject: [PATCH 1/5] Exclude conditions in uninitialized templates The type of conditions in uninitialized templates is unknown which leads to false positives. --- .../NonBooleanIterationStmt.qll | 2 ++ .../test/rules/nonbooleaniterationstmt/test.cpp | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll b/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll index c342811c52..3ca038ce07 100644 --- a/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll +++ b/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll @@ -18,6 +18,8 @@ query predicate problems(Loop loopStmt, string message) { not explicitConversionType instanceof BoolType and //exclude any generated conditions not condition.isCompilerGenerated() and + // exclude any conditions in uninstantiated templates, because their type will be unknown. + not condition.isFromUninstantiatedTemplate(_) and message = "Iteration condition has non boolean type " + explicitConversionType + "." ) } diff --git a/cpp/common/test/rules/nonbooleaniterationstmt/test.cpp b/cpp/common/test/rules/nonbooleaniterationstmt/test.cpp index ed25cad311..04afb042b1 100644 --- a/cpp/common/test/rules/nonbooleaniterationstmt/test.cpp +++ b/cpp/common/test/rules/nonbooleaniterationstmt/test.cpp @@ -41,4 +41,17 @@ class ClassC { if (!d.empty()) { // COMPLIANT } } -}; \ No newline at end of file +}; + +#include +template void test_fp_reported_in_10a(std::vector &p1) { + for (typename std::vector::iterator it = p1.begin(); it != p1.end(); + ++it) { // COMPLIANT + (*it)++; + } +} + +void test_fp_reported_in_10b() { + std::vector vl1; + test_fp_reported_in_10a(vl1); +} \ No newline at end of file From 07840ddd448262be371328246a08de8367b3865c Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 22 Feb 2024 15:17:21 -0800 Subject: [PATCH 2/5] Add test case for FP reported in #10 The query already deals with the case where the condition is in an uninitialized template. --- cpp/common/test/rules/nonbooleanifstmt/test.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cpp/common/test/rules/nonbooleanifstmt/test.cpp b/cpp/common/test/rules/nonbooleanifstmt/test.cpp index b10cd7034e..e17f294a2f 100644 --- a/cpp/common/test/rules/nonbooleanifstmt/test.cpp +++ b/cpp/common/test/rules/nonbooleanifstmt/test.cpp @@ -46,3 +46,16 @@ void test_boolean_conditions() { if (a) { // COMPLIANT - a has an explicit operator bool() } } + +template bool test_fp_reported_in_10a(T &p1) { + if (p1.length() > 10) { // COMPLIANT + return true; + } + return false; +} + +#include +void test_fp_reported_in_10b() { + std::string s; + test_fp_reported_in_10a(s); +} \ No newline at end of file From 11bac20ae05a1a1d7f5c389185dd7521a40f9cd6 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 22 Feb 2024 15:28:13 -0800 Subject: [PATCH 3/5] Reformat alert message according to style guide --- ...orTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql | 2 +- ...ogicalAndOrTheLogicalOperatorsShallHaveTypeBool.expected | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/autosar/src/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql b/cpp/autosar/src/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql index 9414e85956..03b4ae7f1c 100644 --- a/cpp/autosar/src/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql +++ b/cpp/autosar/src/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql @@ -29,4 +29,4 @@ where rt = t.getUnderlyingType().getUnspecifiedType() and rt.getBaseType() instanceof BoolType ) and not operand.isFromUninstantiatedTemplate(_) -select operand, "bool operator called with a non-bool operand of type " + t.getName() + "." +select operand, "Call to bool operator with a non-bool operand of type '" + t.getName() + "'." diff --git a/cpp/autosar/test/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.expected b/cpp/autosar/test/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.expected index e64af14ff5..10ca64a558 100644 --- a/cpp/autosar/test/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.expected +++ b/cpp/autosar/test/rules/M5-3-1/EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.expected @@ -1,3 +1,3 @@ -| test.cpp:10:8:10:8 | 0 | bool operator called with a non-bool operand of type int. | -| test.cpp:12:7:12:7 | 0 | bool operator called with a non-bool operand of type int. | -| test.cpp:12:13:12:17 | ... + ... | bool operator called with a non-bool operand of type int. | +| test.cpp:10:8:10:8 | 0 | Call to bool operator with a non-bool operand of type 'int'. | +| test.cpp:12:7:12:7 | 0 | Call to bool operator with a non-bool operand of type 'int'. | +| test.cpp:12:13:12:17 | ... + ... | Call to bool operator with a non-bool operand of type 'int'. | From c101419a3c597669c846247a85b6177820d3cdf1 Mon Sep 17 00:00:00 2001 From: Remco Vermeulen Date: Thu, 22 Feb 2024 15:33:15 -0800 Subject: [PATCH 4/5] Add changenote --- .../2024-02-22-fix-fp-a5-0-2-and-change-alert-m5-3-1.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 change_notes/2024-02-22-fix-fp-a5-0-2-and-change-alert-m5-3-1.md diff --git a/change_notes/2024-02-22-fix-fp-a5-0-2-and-change-alert-m5-3-1.md b/change_notes/2024-02-22-fix-fp-a5-0-2-and-change-alert-m5-3-1.md new file mode 100644 index 0000000000..86d4ce46ba --- /dev/null +++ b/change_notes/2024-02-22-fix-fp-a5-0-2-and-change-alert-m5-3-1.md @@ -0,0 +1,4 @@ +- `A5-0-2` - `NonBooleanIterationCondition.ql`: + - Address FP reported in #10. Exclude conditions in uninstantiated templates. +- `M5-3-1` - `EachOperandOfTheOperatorTheLogicalAndOrTheLogicalOperatorsShallHaveTypeBool.ql`: + - Adjust the alert message to comply with the style guide. \ No newline at end of file From 4bb4222fa9d98d3a16998a3e1fb1f4703d141542 Mon Sep 17 00:00:00 2001 From: Nicolas Kraiouchkine Date: Wed, 28 Feb 2024 11:14:14 +0100 Subject: [PATCH 5/5] Fix comments in NonBooleanIterationStmt.qll --- .../rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll b/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll index 3ca038ce07..83e58f72d5 100644 --- a/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll +++ b/cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll @@ -1,5 +1,5 @@ /** - * Provides a library which includes a `problems` predicate for reporting.... + * Provides a library which includes a `problems` predicate for reporting non-boolean iteration conditions. */ import cpp @@ -16,7 +16,7 @@ query predicate problems(Loop loopStmt, string message) { condition = loopStmt.getCondition() and explicitConversionType = condition.getExplicitlyConverted().getType().getUnspecifiedType() and not explicitConversionType instanceof BoolType and - //exclude any generated conditions + // exclude any generated conditions not condition.isCompilerGenerated() and // exclude any conditions in uninstantiated templates, because their type will be unknown. not condition.isFromUninstantiatedTemplate(_) and