Skip to content

Address FP reported in #10 #543

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
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "'."
Original file line number Diff line number Diff line change
@@ -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'. |
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,8 +16,10 @@ 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
message = "Iteration condition has non boolean type " + explicitConversionType + "."
)
}
13 changes: 13 additions & 0 deletions cpp/common/test/rules/nonbooleanifstmt/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ void test_boolean_conditions() {
if (a) { // COMPLIANT - a has an explicit operator bool()
}
}

template <typename T> bool test_fp_reported_in_10a(T &p1) {
if (p1.length() > 10) { // COMPLIANT
return true;
}
return false;
}

#include <string>
void test_fp_reported_in_10b() {
std::string s;
test_fp_reported_in_10a(s);
}
15 changes: 14 additions & 1 deletion cpp/common/test/rules/nonbooleaniterationstmt/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ class ClassC {
if (!d.empty()) { // COMPLIANT
}
}
};
};

#include <vector>
template <typename T> void test_fp_reported_in_10a(std::vector<T> &p1) {
for (typename std::vector<T>::iterator it = p1.begin(); it != p1.end();
++it) { // COMPLIANT
(*it)++;
}
}

void test_fp_reported_in_10b() {
std::vector<int> vl1;
test_fp_reported_in_10a(vl1);
}