Skip to content

Commit 7a28f02

Browse files
committed
Rule 18.5.2 - Use BannedFunctions library
Use library to avoid duplication.
1 parent c607798 commit 7a28f02

File tree

1 file changed

+7
-37
lines changed

1 file changed

+7
-37
lines changed

cpp/misra/src/rules/RULE-18-5-2/AvoidProgramTerminatingFunctions.ql

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,25 @@
1616
import cpp
1717
import codingstandards.cpp.misra
1818
import codingstandards.cpp.AlertReporting
19+
import codingstandards.cpp.BannedFunctions
1920

20-
predicate isTerminatingFunction(Function f, string functionName) {
21-
functionName = f.getName() and
22-
(
23-
functionName in ["abort", "exit", "_Exit", "quick_exit"] and
24-
(f.hasQualifiedName("", functionName) or f.hasQualifiedName("std", functionName))
21+
class TerminatingFunction extends Function {
22+
TerminatingFunction() {
23+
this.hasQualifiedName(["", "std"], ["abort", "exit", "_Exit", "quick_exit"])
2524
or
2625
// std::terminate does not occur in the global namespace.
27-
functionName = "terminate" and f.hasQualifiedName("std", functionName)
28-
)
29-
}
30-
31-
class TerminatingFunctionUse extends Expr {
32-
string action;
33-
string functionName;
34-
35-
TerminatingFunctionUse() {
36-
exists(Function f | isTerminatingFunction(f, functionName) |
37-
this.(FunctionCall).getTarget() = f and
38-
action = "Call to"
39-
or
40-
this.(FunctionAccess).getTarget() = f and
41-
action = "Address taken for"
42-
)
43-
}
44-
45-
string getFunctionName() { result = functionName }
46-
47-
string getAction() { result = action }
48-
49-
Element getPrimaryElement() {
50-
// If this is defined in a macro in the users source location, then report the macro
51-
// expansion, otherwise report the element itself. This ensures that we always report
52-
// the use of the terminating function, but combine usages when the macro is defined
53-
// by the user.
54-
exists(Element e | e = MacroUnwrapper<TerminatingFunctionUse>::unwrapElement(this) |
55-
if exists(e.getFile().getRelativePath()) then result = e else result = this
56-
)
26+
this.hasQualifiedName("std", "terminate")
5727
}
5828
}
5929

60-
predicate isInAssertMacroInvocation(TerminatingFunctionUse use) {
30+
predicate isInAssertMacroInvocation(BannedFunctions<TerminatingFunction>::UseExpr use) {
6131
exists(MacroInvocation mi |
6232
mi.getMacroName() = "assert" and
6333
mi.getAnExpandedElement() = use
6434
)
6535
}
6636

67-
from TerminatingFunctionUse use
37+
from BannedFunctions<TerminatingFunction>::UseExpr use
6838
where
6939
not isExcluded(use, BannedAPIsPackage::avoidProgramTerminatingFunctionsQuery()) and
7040
// Exclude the uses in the assert macro

0 commit comments

Comments
 (0)