@@ -3,6 +3,7 @@ import semmle.code.cpp.models.interfaces.Taint
3
3
import semmle.code.cpp.models.interfaces.Alias
4
4
import semmle.code.cpp.models.interfaces.SideEffect
5
5
6
+ /** Pure string functions. */
6
7
class PureStrFunction extends AliasFunction , ArrayFunction , TaintFunction , SideEffectFunction {
7
8
PureStrFunction ( ) {
8
9
hasGlobalOrStdName ( [
@@ -58,6 +59,7 @@ class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunction, SideE
58
59
}
59
60
}
60
61
62
+ /** String standard `strlen` function, and related functions for computing string lengths. */
61
63
class StrLenFunction extends AliasFunction , ArrayFunction , SideEffectFunction {
62
64
StrLenFunction ( ) {
63
65
hasGlobalOrStdName ( [ "strlen" , "strnlen" , "wcslen" ] )
@@ -91,6 +93,7 @@ class StrLenFunction extends AliasFunction, ArrayFunction, SideEffectFunction {
91
93
}
92
94
}
93
95
96
+ /** Pure functions. */
94
97
class PureFunction extends TaintFunction , SideEffectFunction {
95
98
PureFunction ( ) { hasGlobalOrStdName ( [ "abs" , "labs" ] ) }
96
99
@@ -106,3 +109,49 @@ class PureFunction extends TaintFunction, SideEffectFunction {
106
109
107
110
override predicate hasOnlySpecificWriteSideEffects ( ) { any ( ) }
108
111
}
112
+
113
+ /** Pure raw-memory functions. */
114
+ class PureMemFunction extends AliasFunction , ArrayFunction , TaintFunction , SideEffectFunction {
115
+ PureMemFunction ( ) { hasGlobalOrStdName ( [ "memchr" , "memrchr" , "rawmemchr" , "memcmp" , "memmem" ] ) }
116
+
117
+ override predicate hasArrayInput ( int bufParam ) {
118
+ getParameter ( bufParam ) .getUnspecifiedType ( ) instanceof PointerType
119
+ }
120
+
121
+ override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
122
+ exists ( ParameterIndex i |
123
+ input .isParameter ( i ) and
124
+ exists ( getParameter ( i ) )
125
+ or
126
+ input .isParameterDeref ( i ) and
127
+ getParameter ( i ) .getUnspecifiedType ( ) instanceof PointerType
128
+ ) and
129
+ (
130
+ output .isReturnValueDeref ( ) and
131
+ getUnspecifiedType ( ) instanceof PointerType
132
+ or
133
+ output .isReturnValue ( )
134
+ )
135
+ }
136
+
137
+ override predicate parameterNeverEscapes ( int i ) {
138
+ getParameter ( i ) .getUnspecifiedType ( ) instanceof PointerType and
139
+ not parameterEscapesOnlyViaReturn ( i )
140
+ }
141
+
142
+ override predicate parameterEscapesOnlyViaReturn ( int i ) {
143
+ i = 0 and
144
+ getUnspecifiedType ( ) instanceof PointerType
145
+ }
146
+
147
+ override predicate parameterIsAlwaysReturned ( int i ) { none ( ) }
148
+
149
+ override predicate hasOnlySpecificReadSideEffects ( ) { any ( ) }
150
+
151
+ override predicate hasOnlySpecificWriteSideEffects ( ) { any ( ) }
152
+
153
+ override predicate hasSpecificReadSideEffect ( ParameterIndex i , boolean buffer ) {
154
+ getParameter ( i ) .getUnspecifiedType ( ) instanceof PointerType and
155
+ buffer = true
156
+ }
157
+ }
0 commit comments