-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathUngetcCallOnStreamPositionZero.ql
More file actions
81 lines (71 loc) · 2.57 KB
/
UngetcCallOnStreamPositionZero.ql
File metadata and controls
81 lines (71 loc) · 2.57 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @id c/misra/ungetc-call-on-stream-position-zero
* @name RULE-1-5: Disallowed obsolescent usage of 'ungetc' on a file stream at position zero
* @description Calling the function 'ungetc' on a file stream with a position of zero is an
* obsolescent language feature.
* @kind path-problem
* @precision high
* @problem.severity error
* @tags external/misra/id/rule-1-5
* external/misra/c/2012/amendment3
* security
* maintainability
* external/misra/obligation/required
*/
import cpp
import semmle.code.cpp.dataflow.new.DataFlow
import semmle.code.cpp.controlflow.Dominance
import codingstandards.c.misra
/**
* This is an inconclusive list, which is adequate, as RULE-21-3 provides
* assurance we won't have false negatives, or care too much about false
* positives.
*/
class MoveStreamPositionCall extends FunctionCall {
Expr streamArgument;
MoveStreamPositionCall() {
getTarget().hasGlobalOrStdName("fgetc") and
streamArgument = getArgument(0)
or
getTarget().hasGlobalOrStdName("getc") and
streamArgument = getArgument(0)
or
getTarget().hasGlobalOrStdName("fget") and
streamArgument = getArgument(2)
or
getTarget().hasGlobalOrStdName("fscanf") and
streamArgument = getArgument(0)
or
getTarget().hasGlobalOrStdName("fsetpos") and
streamArgument = getArgument(0)
or
getTarget().hasGlobalOrStdName("fseek") and
streamArgument = getArgument(0)
or
getTarget().hasGlobalOrStdName("fread") and
streamArgument = getArgument(3)
}
Expr getStreamArgument() { result = streamArgument }
}
module FilePositionZeroFlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) {
node.asIndirectExpr().(FunctionCall).getTarget().hasGlobalOrStdName("fopen")
}
predicate isSink(DataFlow::Node node) {
exists(FunctionCall fc |
fc.getTarget().hasGlobalOrStdName("ungetc") and
node.asIndirectExpr() = fc.getArgument(1)
)
}
predicate isBarrierIn(DataFlow::Node node) {
exists(MoveStreamPositionCall fc | node.asIndirectExpr() = fc.getStreamArgument())
}
}
module FilePositionZeroFlow = DataFlow::Global<FilePositionZeroFlowConfig>;
import FilePositionZeroFlow::PathGraph
from FilePositionZeroFlow::PathNode sink, FilePositionZeroFlow::PathNode source
where
not isExcluded(sink.getNode().asExpr(), Language4Package::ungetcCallOnStreamPositionZeroQuery()) and
FilePositionZeroFlow::flowPath(source, sink)
select sink.getNode(), source, sink,
"Obsolescent call to ungetc on file stream $@ at position zero.", source, source.toString()