Skip to content

Commit 43e8b90

Browse files
committed
C++: Fix 'strtol' model.
1 parent d6b6c43 commit 43e8b90

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private import implementations.Strtok
1919
private import implementations.Strset
2020
private import implementations.Strcrement
2121
private import implementations.Strnextc
22+
private import implementations.Strtol
2223
private import implementations.StdContainer
2324
private import implementations.StdPair
2425
private import implementations.StdMap

cpp/ql/lib/semmle/code/cpp/models/implementations/Pure.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunctio
1313
PureStrFunction() {
1414
this.hasGlobalOrStdOrBslName([
1515
atoi(), "strcasestr", "strchnul", "strchr", "strchrnul", "strstr", "strpbrk", "strrchr",
16-
"strspn", strtol(), strrev(), strcmp(), strlwr(), strupr()
16+
"strspn", strrev(), strcmp(), strlwr(), strupr()
1717
])
1818
}
1919

@@ -70,8 +70,6 @@ private class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunctio
7070

7171
private string atoi() { result = ["atof", "atoi", "atol", "atoll"] }
7272

73-
private string strtol() { result = ["strtod", "strtof", "strtol", "strtoll", "strtoq", "strtoul"] }
74-
7573
private string strlwr() {
7674
result = ["_strlwr", "_wcslwr", "_mbslwr", "_strlwr_l", "_wcslwr_l", "_mbslwr_l"]
7775
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import semmle.code.cpp.models.interfaces.ArrayFunction
2+
import semmle.code.cpp.models.interfaces.Taint
3+
import semmle.code.cpp.models.interfaces.Alias
4+
import semmle.code.cpp.models.interfaces.SideEffect
5+
6+
private string strtol() { result = ["strtod", "strtof", "strtol", "strtoll", "strtoq", "strtoul"] }
7+
8+
/**
9+
* The standard function `strtol` and its assorted variants
10+
*/
11+
private class Strtol extends AliasFunction, ArrayFunction, TaintFunction, SideEffectFunction {
12+
Strtol() { this.hasGlobalOrStdOrBslName(strtol()) }
13+
14+
override predicate hasArrayInput(int bufParam) {
15+
// All the functions given by `strtol()` takes a `const char*` input as the first parameter
16+
bufParam = 0
17+
}
18+
19+
override predicate hasArrayWithNullTerminator(int bufParam) { bufParam = 0 }
20+
21+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
22+
(
23+
input.isParameter(0)
24+
or
25+
input.isParameterDeref(0)
26+
) and
27+
output.isReturnValue()
28+
}
29+
30+
override predicate parameterNeverEscapes(int i) {
31+
this.getParameter(i).getUnspecifiedType() instanceof PointerType
32+
}
33+
34+
override predicate parameterEscapesOnlyViaReturn(int i) { none() }
35+
36+
override predicate parameterIsAlwaysReturned(int i) { none() }
37+
38+
override predicate hasOnlySpecificReadSideEffects() { any() }
39+
40+
override predicate hasOnlySpecificWriteSideEffects() { any() }
41+
42+
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) {
43+
i = 0 and
44+
buffer = true
45+
}
46+
47+
override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) {
48+
i = 1 and buffer = false and mustWrite = false
49+
}
50+
}

0 commit comments

Comments
 (0)