-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCheckingReturnValues.ql
More file actions
42 lines (39 loc) · 1.39 KB
/
CheckingReturnValues.ql
File metadata and controls
42 lines (39 loc) · 1.39 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
/**
* @name Unchecked return value
* @description The return value of each non-void function call should be checked for error conditions, or cast to (void) if irrelevant.
* @kind problem
* @id cpp/jpl-c/checking-return-values
* @problem.severity warning
* @tags correctness
* reliability
* external/jpl
*/
import cpp
/**
* In its full generality, the rule applies to all functions that
* return non-void, including things like 'printf' and 'close',
* which are routinely not checked because the behavior on success
* is the same as the behavior on failure. The recommendation is
* to add an explicit cast to void for such functions. For code
* bases that have not been developed with this rule in mind, at
* least for such commonly ignored functions, it may be better to
* add them as exceptions to this whitelist predicate.
*/
predicate whitelist(Function f) {
// Example:
// f.hasName("printf") or f.hasName("close") or // ...
none()
}
from FunctionCall c, string msg
where
not c.getTarget().getType() instanceof VoidType and
not whitelist(c.getTarget()) and
(
c instanceof ExprInVoidContext and
msg = "The return value of non-void function $@ is not checked."
or
definition(_, c.getParent()) and
not definitionUsePair(_, c.getParent(), _) and
msg = "$@'s return value is stored but not checked."
)
select c, msg, c.getTarget() as f, f.getName()