Skip to content

Commit dca39f0

Browse files
authored
Merge pull request github#2027 from zlaski-semmle/zlaski/memset-model
[zlaski/memset-model] QL model for `memset` and friends
2 parents 3e6f8fb + a0cbd87 commit dca39f0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

cpp/ql/src/semmle/code/cpp/models/Models.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
private import implementations.IdentityFunction
22
private import implementations.Inet
33
private import implementations.Memcpy
4+
private import implementations.Memset
45
private import implementations.Printf
56
private import implementations.Pure
67
private import implementations.Strcat
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import semmle.code.cpp.Function
2+
import semmle.code.cpp.models.interfaces.ArrayFunction
3+
import semmle.code.cpp.models.interfaces.DataFlow
4+
import semmle.code.cpp.models.interfaces.Alias
5+
6+
/**
7+
* The standard function `memset` and its assorted variants
8+
*/
9+
class MemsetFunction extends ArrayFunction, DataFlowFunction, AliasFunction {
10+
MemsetFunction() {
11+
hasGlobalName("memset") or
12+
hasGlobalName("wmemset") or
13+
hasGlobalName("bzero") or
14+
hasGlobalName("__builtin_memset") or
15+
hasGlobalName("__builtin_memset_chk") or
16+
hasQualifiedName("std", "memset") or
17+
hasQualifiedName("std", "wmemset")
18+
}
19+
20+
override predicate hasArrayOutput(int bufParam) { bufParam = 0 }
21+
22+
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
23+
input.isParameter(0) and
24+
output.isReturnValue()
25+
}
26+
27+
override predicate hasArrayWithVariableSize(int bufParam, int countParam) {
28+
bufParam = 0 and
29+
(if hasGlobalName("bzero") then countParam = 1 else countParam = 2)
30+
}
31+
32+
override predicate parameterNeverEscapes(int index) { hasGlobalName("bzero") and index = 0 }
33+
34+
override predicate parameterEscapesOnlyViaReturn(int index) {
35+
not hasGlobalName("bzero") and index = 0
36+
}
37+
38+
override predicate parameterIsAlwaysReturned(int index) {
39+
not hasGlobalName("bzero") and index = 0
40+
}
41+
}

0 commit comments

Comments
 (0)