@@ -14,6 +14,43 @@ class StdBasicString extends TemplateClass {
1414 StdBasicString ( ) { this .hasQualifiedName ( "std" , "basic_string" ) }
1515}
1616
17+ /**
18+ * Additional model for `std::string` constructors that reference the character
19+ * type of the container, or an iterator. For example construction from
20+ * iterators:
21+ * ```
22+ * std::string b(a.begin(), a.end());
23+ * ```
24+ */
25+ class StdStringConstructor extends Constructor , TaintFunction {
26+ StdStringConstructor ( ) { this .getDeclaringType ( ) .hasQualifiedName ( "std" , "basic_string" ) }
27+
28+ /**
29+ * Gets the index of a parameter to this function that is a string (or
30+ * character).
31+ */
32+ int getAStringParameterIndex ( ) {
33+ getParameter ( result ) .getType ( ) instanceof PointerType or // e.g. `std::basic_string::CharT *`
34+ getParameter ( result ) .getType ( ) instanceof ReferenceType or // e.g. `std::basic_string &`
35+ getParameter ( result ) .getUnspecifiedType ( ) =
36+ getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( ) // i.e. `std::basic_string::CharT`
37+ }
38+
39+ /**
40+ * Gets the index of a parameter to this function that is an iterator.
41+ */
42+ int getAnIteratorParameterIndex ( ) { getParameter ( result ) .getType ( ) instanceof Iterator }
43+
44+ override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
45+ // taint flow from any parameter of the value type to the returned object
46+ (
47+ input .isParameterDeref ( getAStringParameterIndex ( ) ) or
48+ input .isParameter ( getAnIteratorParameterIndex ( ) )
49+ ) and
50+ output .isReturnValue ( ) // TODO: this should be `isQualifierObject` by our current definitions, but that flow is not yet supported.
51+ }
52+ }
53+
1754/**
1855 * The `std::string` function `c_str`.
1956 */
@@ -79,8 +116,8 @@ class StdStringAppend extends TaintFunction {
79116 * character).
80117 */
81118 int getAStringParameterIndex ( ) {
82- getParameter ( result ) .getType ( ) instanceof PointerType or
83- getParameter ( result ) .getType ( ) instanceof ReferenceType or
119+ getParameter ( result ) .getType ( ) instanceof PointerType or // e.g. `std::basic_string::CharT *`
120+ getParameter ( result ) .getType ( ) instanceof ReferenceType or // e.g. `std::basic_string &`
84121 getParameter ( result ) .getUnspecifiedType ( ) =
85122 getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( ) // i.e. `std::basic_string::CharT`
86123 }
@@ -115,15 +152,23 @@ class StdStringAssign extends TaintFunction {
115152 * character).
116153 */
117154 int getAStringParameterIndex ( ) {
118- getParameter ( result ) .getType ( ) instanceof PointerType or
119- getParameter ( result ) .getType ( ) instanceof ReferenceType or
155+ getParameter ( result ) .getType ( ) instanceof PointerType or // e.g. `std::basic_string::CharT *`
156+ getParameter ( result ) .getType ( ) instanceof ReferenceType or // e.g. `std::basic_string &`
120157 getParameter ( result ) .getUnspecifiedType ( ) =
121158 getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( ) // i.e. `std::basic_string::CharT`
122159 }
123160
161+ /**
162+ * Gets the index of a parameter to this function that is an iterator.
163+ */
164+ int getAnIteratorParameterIndex ( ) { getParameter ( result ) .getType ( ) instanceof Iterator }
165+
124166 override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
125167 // flow from parameter to string itself (qualifier) and return value
126- input .isParameterDeref ( getAStringParameterIndex ( ) ) and
168+ (
169+ input .isParameterDeref ( getAStringParameterIndex ( ) ) or
170+ input .isParameter ( getAnIteratorParameterIndex ( ) )
171+ ) and
127172 (
128173 output .isQualifierObject ( ) or
129174 output .isReturnValueDeref ( )
@@ -137,14 +182,9 @@ class StdStringAssign extends TaintFunction {
137182 */
138183class StdStringBeginEnd extends TaintFunction {
139184 StdStringBeginEnd ( ) {
140- this .hasQualifiedName ( "std" , "basic_string" , "begin" ) or
141- this .hasQualifiedName ( "std" , "basic_string" , "cbegin" ) or
142- this .hasQualifiedName ( "std" , "basic_string" , "rbegin" ) or
143- this .hasQualifiedName ( "std" , "basic_string" , "crbegin" ) or
144- this .hasQualifiedName ( "std" , "basic_string" , "end" ) or
145- this .hasQualifiedName ( "std" , "basic_string" , "cend" ) or
146- this .hasQualifiedName ( "std" , "basic_string" , "rend" ) or
147- this .hasQualifiedName ( "std" , "basic_string" , "crend" )
185+ this
186+ .hasQualifiedName ( "std" , "basic_string" ,
187+ [ "begin" , "cbegin" , "rbegin" , "crbegin" , "end" , "cend" , "rend" , "crend" ] )
148188 }
149189
150190 override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
0 commit comments