|
| 1 | +/** |
| 2 | + * @id c/misra/ungetc-call-on-stream-position-zero |
| 3 | + * @name RULE-1-5: Disallowed obsolescent usage of 'ungetc' on a file stream at position zero |
| 4 | + * @description Calling the function 'ungetc' on a file stream with a position of zero is an |
| 5 | + * obsolescent language feature. |
| 6 | + * @kind path-problem |
| 7 | + * @precision high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-1-5 |
| 10 | + * external/misra/c/2012/amendment3 |
| 11 | + * security |
| 12 | + * maintainability |
| 13 | + * external/misra/obligation/required |
| 14 | + */ |
| 15 | + |
| 16 | +import cpp |
| 17 | +import semmle.code.cpp.dataflow.new.DataFlow |
| 18 | +import semmle.code.cpp.controlflow.Dominance |
| 19 | +import codingstandards.c.misra |
| 20 | + |
| 21 | +/** |
| 22 | + * This is an inconclusive list, which is adequate, as RULE-21-3 provides |
| 23 | + * assurance we won't have false negatives, or care too much about false |
| 24 | + * positives. |
| 25 | + */ |
| 26 | +class MoveStreamPositionCall extends FunctionCall { |
| 27 | + Expr streamArgument; |
| 28 | + |
| 29 | + MoveStreamPositionCall() { |
| 30 | + getTarget().hasGlobalOrStdName("fgetc") and |
| 31 | + streamArgument = getArgument(0) |
| 32 | + or |
| 33 | + getTarget().hasGlobalOrStdName("getc") and |
| 34 | + streamArgument = getArgument(0) |
| 35 | + or |
| 36 | + getTarget().hasGlobalOrStdName("fget") and |
| 37 | + streamArgument = getArgument(2) |
| 38 | + or |
| 39 | + getTarget().hasGlobalOrStdName("fscanf") and |
| 40 | + streamArgument = getArgument(0) |
| 41 | + or |
| 42 | + getTarget().hasGlobalOrStdName("fsetpos") and |
| 43 | + streamArgument = getArgument(0) |
| 44 | + or |
| 45 | + getTarget().hasGlobalOrStdName("fseek") and |
| 46 | + streamArgument = getArgument(0) |
| 47 | + or |
| 48 | + getTarget().hasGlobalOrStdName("fread") and |
| 49 | + streamArgument = getArgument(3) |
| 50 | + } |
| 51 | + |
| 52 | + Expr getStreamArgument() { result = streamArgument } |
| 53 | +} |
| 54 | + |
| 55 | +module FilePositionZeroFlowConfig implements DataFlow::ConfigSig { |
| 56 | + predicate isSource(DataFlow::Node node) { |
| 57 | + node.asIndirectExpr().(FunctionCall).getTarget().hasGlobalOrStdName("fopen") |
| 58 | + } |
| 59 | + |
| 60 | + predicate isSink(DataFlow::Node node) { |
| 61 | + exists(FunctionCall fc | |
| 62 | + fc.getTarget().hasGlobalOrStdName("ungetc") and |
| 63 | + node.asIndirectExpr() = fc.getArgument(1) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + predicate isBarrierIn(DataFlow::Node node) { |
| 68 | + exists(MoveStreamPositionCall fc | node.asIndirectExpr() = fc.getStreamArgument()) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +module FilePositionZeroFlow = DataFlow::Global<FilePositionZeroFlowConfig>; |
| 73 | + |
| 74 | +import FilePositionZeroFlow::PathGraph |
| 75 | + |
| 76 | +from FilePositionZeroFlow::PathNode sink, FilePositionZeroFlow::PathNode source |
| 77 | +where |
| 78 | + not isExcluded(sink.getNode().asExpr(), Language4Package::ungetcCallOnStreamPositionZeroQuery()) and |
| 79 | + FilePositionZeroFlow::flowPath(source, sink) |
| 80 | +select sink.getNode(), source, sink, |
| 81 | + "Obsolescent call to ungetc on file stream $@ at position zero.", source, source.toString() |
0 commit comments