|
| 1 | +/** |
| 2 | + * Provides implementation classes modeling `strset` 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.ArrayFunction |
| 8 | +import semmle.code.cpp.models.interfaces.DataFlow |
| 9 | +import semmle.code.cpp.models.interfaces.Alias |
| 10 | +import semmle.code.cpp.models.interfaces.SideEffect |
| 11 | + |
| 12 | +/** |
| 13 | + * The standard function `strset` and its assorted variants |
| 14 | + */ |
| 15 | +private class StrsetFunction extends ArrayFunction, DataFlowFunction, AliasFunction, |
| 16 | + SideEffectFunction { |
| 17 | + StrsetFunction() { |
| 18 | + hasGlobalName([ |
| 19 | + "strset", "_strset", "_strset_l", "_wcsset", "_wcsset_l", "_mbsset", "_mbsset_l", |
| 20 | + "_mbsnbset", "_mbsnbset_l", "_strnset", "_strnset_l", "_wcsnset", "_wcsnset_l", "_mbsnset", |
| 21 | + "_mbsnset_l" |
| 22 | + ]) |
| 23 | + } |
| 24 | + |
| 25 | + override predicate hasArrayWithNullTerminator(int bufParam) { bufParam = 0 } |
| 26 | + |
| 27 | + override predicate hasArrayInput(int bufParam) { bufParam = 0 } |
| 28 | + |
| 29 | + override predicate hasArrayOutput(int bufParam) { bufParam = 0 } |
| 30 | + |
| 31 | + override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { |
| 32 | + // flow from the character that overrides the string |
| 33 | + input.isParameter(1) and |
| 34 | + ( |
| 35 | + output.isReturnValueDeref() |
| 36 | + or |
| 37 | + output.isParameterDeref(0) |
| 38 | + ) |
| 39 | + or |
| 40 | + // flow from the input string to the output string |
| 41 | + input.isParameter(0) and |
| 42 | + output.isReturnValue() |
| 43 | + } |
| 44 | + |
| 45 | + override predicate parameterNeverEscapes(int index) { none() } |
| 46 | + |
| 47 | + override predicate parameterEscapesOnlyViaReturn(int index) { index = 0 } |
| 48 | + |
| 49 | + override predicate parameterIsAlwaysReturned(int index) { index = 0 } |
| 50 | + |
| 51 | + override predicate hasOnlySpecificReadSideEffects() { any() } |
| 52 | + |
| 53 | + override predicate hasOnlySpecificWriteSideEffects() { any() } |
| 54 | + |
| 55 | + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { |
| 56 | + i = 0 and buffer = true and mustWrite = true |
| 57 | + } |
| 58 | + |
| 59 | + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { |
| 60 | + i = 0 and buffer = true |
| 61 | + } |
| 62 | +} |
0 commit comments