11import cpp
22import semmle.code.cpp.models.interfaces.Taint
3+ import semmle.code.cpp.models.interfaces.DataFlow
34
45class IteratorTraits extends Class {
56 IteratorTraits ( ) {
@@ -25,12 +26,17 @@ class IteratorByTypedefs extends Class {
2526 }
2627}
2728
28- class IteratorByStdIteratorTraits extends Type { }
29+ class StdIterator extends Class {
30+ StdIterator ( ) {
31+ this .hasQualifiedName ( "std" , "iterator" )
32+ }
33+ }
2934
3035class LegacyIterator extends Type {
3136 LegacyIterator ( ) {
3237 this instanceof IteratorByTypedefs or
33- exists ( IteratorTraits it | it .getIteratorType ( ) = this )
38+ exists ( IteratorTraits it | it .getIteratorType ( ) = this ) or
39+ this instanceof StdIterator
3440 }
3541}
3642
@@ -47,7 +53,7 @@ class IteratorPointerDereferenceOperator extends Operator, TaintFunction {
4753 }
4854}
4955
50- class IteratorCrementOperator extends Operator , TaintFunction {
56+ class IteratorCrementOperator extends Operator , DataFlowFunction , TaintFunction {
5157 IteratorCrementOperator ( ) {
5258 (
5359 this .hasName ( "operator++" ) or
@@ -62,6 +68,11 @@ class IteratorCrementOperator extends Operator, TaintFunction {
6268 .getBaseType ( ) instanceof LegacyIterator
6369 }
6470
71+ override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
72+ input .isParameter ( 0 ) and
73+ output .isReturnValue ( )
74+ }
75+
6576 override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
6677 input .isParameterDeref ( 0 ) and
6778 output .isParameterDeref ( 0 )
@@ -75,7 +86,7 @@ class IteratorFieldOperator extends Operator, TaintFunction {
7586 .getACallToThisFunction ( )
7687 .getArgument ( 0 )
7788 .getFullyConverted ( )
78- .getUnderlyingType ( )
89+ .getUnspecifiedType ( )
7990 .( PointerType )
8091 .getBaseType ( ) instanceof LegacyIterator
8192 }
@@ -88,24 +99,22 @@ class IteratorFieldOperator extends Operator, TaintFunction {
8899
89100class IteratorAddOperator extends Operator , TaintFunction {
90101 IteratorAddOperator ( ) {
102+ this .hasName ( "operator+" ) and
91103 (
92- this .hasName ( "operator+" )
93- ) and
94- (
95- this
96- .getACallToThisFunction ( )
97- .getArgument ( 0 )
98- .getFullyConverted ( )
99- .getUnderlyingType ( )
100- .( PointerType )
101- .getBaseType ( ) instanceof LegacyIterator or
102- this
103- .getACallToThisFunction ( )
104- .getArgument ( 0 )
105- .getFullyConverted ( )
106- .getUnderlyingType ( )
107- .( PointerType )
108- .getBaseType ( ) instanceof LegacyIterator
104+ this
105+ .getACallToThisFunction ( )
106+ .getArgument ( 0 )
107+ .getFullyConverted ( )
108+ .getUnspecifiedType ( )
109+ .( PointerType )
110+ .getBaseType ( ) instanceof LegacyIterator or
111+ this
112+ .getACallToThisFunction ( )
113+ .getArgument ( 0 )
114+ .getFullyConverted ( )
115+ .getUnspecifiedType ( )
116+ .( PointerType )
117+ .getBaseType ( ) instanceof LegacyIterator
109118 )
110119 }
111120
@@ -118,23 +127,17 @@ class IteratorAddOperator extends Operator, TaintFunction {
118127 }
119128}
120129
130+ /**
131+ * A non-member `operator-` function that takes an iterator as its first argument.
132+ */
121133class IteratorSubOperator extends Operator , TaintFunction {
122134 IteratorSubOperator ( ) {
123- (
124- this .hasName ( "operator-" )
125- ) and
135+ this .hasName ( "operator-" ) and
126136 this
127137 .getACallToThisFunction ( )
128138 .getArgument ( 0 )
129139 .getFullyConverted ( )
130- .getUnderlyingType ( )
131- .( PointerType )
132- .getBaseType ( ) instanceof LegacyIterator and
133- not this
134- .getACallToThisFunction ( )
135- .getArgument ( 1 )
136- .getFullyConverted ( )
137- .getUnderlyingType ( )
140+ .getUnspecifiedType ( )
138141 .( PointerType )
139142 .getBaseType ( ) instanceof LegacyIterator
140143 }
@@ -148,35 +151,26 @@ class IteratorSubOperator extends Operator, TaintFunction {
148151 }
149152}
150153
151- class IteratorDiffOperator extends Operator , TaintFunction {
152- IteratorDiffOperator ( ) {
154+ class IteratorAssignArithmeticOperator extends MemberFunction , DataFlowFunction , TaintFunction {
155+ IteratorAssignArithmeticOperator ( ) {
153156 (
154- this .hasName ( "operator-" )
157+ this .hasName ( "operator+=" ) or
158+ this .hasName ( "operator-=" )
155159 ) and
156- this
157- .getACallToThisFunction ( )
158- .getArgument ( 0 )
159- .getFullyConverted ( )
160- .getUnderlyingType ( )
161- .( PointerType )
162- .getBaseType ( ) instanceof LegacyIterator and
163- not this
164- .getACallToThisFunction ( )
165- .getArgument ( 1 )
166- .getFullyConverted ( )
167- .getUnderlyingType ( )
168- .( PointerType )
169- .getBaseType ( ) instanceof LegacyIterator
160+ this .getDeclaringType ( ) instanceof LegacyIterator
170161 }
171162
172- override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
173- (
174- input .isParameter ( 0 ) or
175- input .isParameter ( 1 )
176- ) and
163+ override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
164+ input .isParameter ( 0 ) and
177165 output .isReturnValue ( )
178166 }
167+
168+ override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
169+ input .isParameterDeref ( 1 ) and
170+ output .isParameterDeref ( 0 )
171+ }
179172}
173+
180174class IteratorPointerDereferenceMemberOperator extends MemberFunction , TaintFunction {
181175 IteratorPointerDereferenceMemberOperator ( ) {
182176 this .hasName ( "operator*" ) and
@@ -189,7 +183,7 @@ class IteratorPointerDereferenceMemberOperator extends MemberFunction, TaintFunc
189183 }
190184}
191185
192- class IteratorCrementMemberOperator extends MemberFunction , TaintFunction {
186+ class IteratorCrementMemberOperator extends MemberFunction , DataFlowFunction , TaintFunction {
193187 IteratorCrementMemberOperator ( ) {
194188 (
195189 this .hasName ( "operator++" ) or
@@ -198,6 +192,11 @@ class IteratorCrementMemberOperator extends MemberFunction, TaintFunction {
198192 this .getDeclaringType ( ) instanceof LegacyIterator
199193 }
200194
195+ override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
196+ input .isQualifierAddress ( ) and
197+ output .isReturnValue ( )
198+ }
199+
201200 override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
202201 input .isQualifierObject ( ) and
203202 output .isQualifierObject ( )
@@ -216,13 +215,14 @@ class IteratorFieldMemberOperator extends Operator, TaintFunction {
216215 }
217216}
218217
219- class IteratorMemberBinaryOperator extends MemberFunction , TaintFunction {
220- IteratorMemberBinaryOperator ( ) {
221- (
222- this .hasName ( "operator+" ) or
223- this .hasName ( "operator-" )
224- ) and
225- this .getDeclaringType ( ) instanceof LegacyIterator
218+ /**
219+ * An `operator+` or `operator-` member function of an iterator class.
220+ */
221+ class IteratorBinaryArithmeticMemberOperator extends MemberFunction , TaintFunction {
222+ IteratorBinaryArithmeticMemberOperator ( ) {
223+ this .hasName ( "operator-" ) and
224+ this .getDeclaringType ( ) instanceof LegacyIterator and
225+ this .getParameter ( 0 ) .getUnspecifiedType ( ) instanceof LegacyIterator
226226 }
227227
228228 override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -234,22 +234,37 @@ class IteratorMemberBinaryOperator extends MemberFunction, TaintFunction {
234234 }
235235}
236236
237- class IteratorMemberAssignOperator extends MemberFunction , TaintFunction {
238- IteratorMemberAssignOperator ( ) {
237+ class IteratorAssignArithmeticMemberOperator extends MemberFunction , DataFlowFunction , TaintFunction {
238+ IteratorAssignArithmeticMemberOperator ( ) {
239239 (
240240 this .hasName ( "operator+=" ) or
241241 this .hasName ( "operator-=" )
242242 ) and
243243 this .getDeclaringType ( ) instanceof LegacyIterator
244244 }
245245
246+ override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
247+ input .isQualifierAddress ( ) and
248+ output .isReturnValue ( )
249+ }
250+
251+ override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
252+ input .isParameter ( 0 ) and
253+ output .isQualifierObject ( )
254+ }
255+ }
256+
257+ class IteratorArrayMemberOperator extends MemberFunction , TaintFunction {
258+ IteratorArrayMemberOperator ( ) {
259+ this .hasName ( "operator[]" ) and
260+ this .getDeclaringType ( ) instanceof LegacyIterator
261+ }
262+
246263 override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
247264 (
248265 input .isQualifierObject ( ) or
249266 input .isParameter ( 0 )
250267 ) and
251- output .isQualifierObject ( )
252- or
253268 output .isReturnValue ( )
254269 }
255- }
270+ }
0 commit comments