14
14
import cpp
15
15
import codingstandards.c.cert
16
16
17
- class ExitFunction extends Function {
18
- ExitFunction ( ) { this .hasGlobalName ( [ "_Exit" , "exit" , "quick_exit" , "longjmp" ] ) }
17
+ /**
18
+ * Exit function or macro.
19
+ */
20
+ class Exit extends Locatable {
21
+ Exit ( ) {
22
+ [ "_Exit" , "exit" , "quick_exit" , "longjmp" ] = [ this .( Function ) .getName ( ) , this .( Macro ) .getName ( ) ]
23
+ }
19
24
}
20
25
21
- class ExitFunctionCall extends FunctionCall {
22
- ExitFunctionCall ( ) { this .getTarget ( ) instanceof ExitFunction }
26
+ class ExitExpr extends Expr {
27
+ ExitExpr ( ) {
28
+ this .( FunctionCall ) .getTarget ( ) instanceof Exit
29
+ or
30
+ any ( MacroInvocation m | this = m .getExpr ( ) ) .getMacro ( ) instanceof Exit
31
+ }
23
32
}
24
33
34
+ /**
35
+ * Functions that are registered as exit handlers.
36
+ */
25
37
class RegisteredAtexit extends FunctionAccess {
26
38
RegisteredAtexit ( ) {
27
39
exists ( FunctionCall ae |
@@ -32,24 +44,26 @@ class RegisteredAtexit extends FunctionAccess {
32
44
}
33
45
34
46
/**
35
- * Nodes of type Function, FunctionCall or FunctionAccess that \
36
- * are reachable from a redistered atexit handler and
47
+ * Nodes of type Function, FunctionCall, FunctionAccess or ExitExpr
48
+ * that are reachable from a registered atexit handler and
37
49
* can reach an exit function.
38
50
*/
39
51
class InterestingNode extends ControlFlowNode {
40
52
InterestingNode ( ) {
41
53
exists ( Function f |
42
54
(
43
55
this = f and
44
- // exit functions are not part of edges
45
- not this = any ( ExitFunction ec )
56
+ // exit is not part of edges
57
+ not this instanceof Exit
46
58
or
47
59
this .( FunctionCall ) .getTarget ( ) = f
48
60
or
49
61
this .( FunctionAccess ) .getTarget ( ) = f
62
+ or
63
+ this .( ExitExpr ) .getEnclosingFunction ( ) = f
50
64
) and
51
- // reaches an exit function
52
- f .calls * ( any ( ExitFunction e ) ) and
65
+ // reaches an `ExitExpr`
66
+ f .calls * ( any ( ExitExpr ee ) . getEnclosingFunction ( ) ) and
53
67
// is reachable from a registered atexit function
54
68
exists ( RegisteredAtexit re | re .getTarget ( ) .calls * ( f ) )
55
69
)
@@ -62,14 +76,12 @@ class InterestingNode extends ControlFlowNode {
62
76
* `Function` and `FunctionCall` in their body.
63
77
*/
64
78
query predicate edges ( InterestingNode a , InterestingNode b ) {
65
- a .( FunctionAccess ) .getTarget ( ) = b
66
- or
67
- a .( FunctionCall ) .getTarget ( ) = b
68
- or
79
+ a .( FunctionAccess ) .getTarget ( ) = b or
80
+ a .( FunctionCall ) .getTarget ( ) = b or
69
81
a .( Function ) .calls ( _, b )
70
82
}
71
83
72
- from RegisteredAtexit hr , Function f , ExitFunctionCall e
84
+ from RegisteredAtexit hr , Function f , ExitExpr e
73
85
where edges ( hr , f ) and edges + ( f , e )
74
86
select f , hr , e , "The function is $@ and $@. It must instead terminate by returning." , hr ,
75
87
"registered as `exit handler`" , e , "calls an `exit function`"
0 commit comments