|
| 1 | +/** |
| 2 | + * Provides implementation classes modeling `fopen` and various similar |
| 3 | + * functions. See `semmle.code.cpp.models.Models` for usage information. |
| 4 | + */ |
| 5 | + |
| 6 | +import semmle.code.cpp.Function |
| 7 | +import semmle.code.cpp.models.interfaces.Alias |
| 8 | +import semmle.code.cpp.models.interfaces.SideEffect |
| 9 | + |
| 10 | +/** The function `fopen` and friends. */ |
| 11 | +private class Fopen extends Function, AliasFunction, SideEffectFunction { |
| 12 | + Fopen() { |
| 13 | + this.hasGlobalOrStdName(["fopen", "fopen_s", "freopen"]) |
| 14 | + or |
| 15 | + this.hasGlobalName(["_open", "_wfopen", "_fsopen", "_wfsopen", "_wopen"]) |
| 16 | + } |
| 17 | + |
| 18 | + override predicate hasOnlySpecificWriteSideEffects() { any() } |
| 19 | + |
| 20 | + override predicate hasOnlySpecificReadSideEffects() { any() } |
| 21 | + |
| 22 | + override predicate parameterEscapesOnlyViaReturn(int i) { none() } |
| 23 | + |
| 24 | + override predicate parameterNeverEscapes(int index) { |
| 25 | + // None of the parameters escape |
| 26 | + this.getParameter(index).getUnspecifiedType() instanceof PointerType |
| 27 | + } |
| 28 | + |
| 29 | + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { |
| 30 | + ( |
| 31 | + this.hasGlobalOrStdName(["fopen", "fopen_s"]) |
| 32 | + or |
| 33 | + this.hasGlobalName(["_wfopen", "_fsopen", "_wfsopen"]) |
| 34 | + ) and |
| 35 | + i = [0, 1] and |
| 36 | + buffer = true |
| 37 | + or |
| 38 | + this.hasGlobalOrStdName("freopen") and |
| 39 | + ( |
| 40 | + i = [0, 1] and |
| 41 | + buffer = true |
| 42 | + or |
| 43 | + i = 2 and |
| 44 | + buffer = false |
| 45 | + ) |
| 46 | + or |
| 47 | + this.hasGlobalName(["_open", "_wopen"]) and |
| 48 | + i = 0 and |
| 49 | + buffer = true |
| 50 | + } |
| 51 | +} |
0 commit comments