-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathMissingCatchAllExceptionHandlerInMain.ql
More file actions
56 lines (52 loc) · 1.84 KB
/
MissingCatchAllExceptionHandlerInMain.ql
File metadata and controls
56 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @id cpp/misra/missing-catch-all-exception-handler-in-main
* @name RULE-18-3-1: There should be at least one exception handler to catch all otherwise unhandled exceptions
* @description The main function should have a catch-all exception handler (catch(...)) to catch
* all otherwise unhandled exceptions.
* @kind problem
* @precision high
* @problem.severity warning
* @tags external/misra/id/rule-18-3-1
* scope/single-translation-unit
* maintainability
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/
import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.exceptions.ExceptionFlow
import codingstandards.cpp.exceptions.ExceptionSpecifications
/**
* A function call in main that is not declared noexcept and is not within a catch-all
* exception handler (catch(...)).
*/
class UncaughtFunctionCallInMain extends FunctionCall {
UncaughtFunctionCallInMain() {
getEnclosingFunction().hasName("main") and
not isNoExceptTrue(getTarget()) and
not exists(TryStmt try |
try = getATryStmt(this.getEnclosingStmt()) and
try.getCatchClause(_) instanceof CatchAnyBlock
)
}
/**
* We only want to report one counter-example indicating a missing catch(...), so this holds only
* for the first one we find.
*/
predicate isFirst() {
this =
rank[1](UncaughtFunctionCallInMain fc |
any()
|
fc order by fc.getLocation().getStartLine(), fc.getLocation().getStartColumn()
)
}
}
from Function f, UncaughtFunctionCallInMain fc
where
not isExcluded(f, Exceptions3Package::missingCatchAllExceptionHandlerInMainQuery()) and
f.getName() = "main" and
fc.isFirst()
select f,
"Main function has a $@ which is not within a try block with a catch-all ('catch(...)') handler.",
fc, "function call that may throw"