Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a57fa2

Browse files
author
Robert Marsh
committed
C++: handle reference args to iterator operators
1 parent 87b6570 commit 2a57fa2

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

  • cpp/ql/src/semmle/code/cpp/models/implementations

cpp/ql/src/semmle/code/cpp/models/implementations/Iterator.qll

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Iterator extends Type {
5757
}
5858
}
5959

60-
private predicate calledWithIteratorArgument(Operator op, int index) {
60+
private FunctionInput getIteratorArgumentInput(Operator op, int index) {
6161
exists(Type t |
6262
t =
6363
op
@@ -67,23 +67,29 @@ private predicate calledWithIteratorArgument(Operator op, int index) {
6767
.getType()
6868
.stripTopLevelSpecifiers()
6969
|
70-
t instanceof Iterator
71-
or
72-
t.(ReferenceType).getBaseType() instanceof Iterator
70+
(
71+
t instanceof Iterator or
72+
t.(ReferenceType).getBaseType() instanceof Iterator
73+
) and
74+
if op.getParameter(index).getUnspecifiedType() instanceof ReferenceType
75+
then result.isParameterDeref(index)
76+
else result.isParameter(index)
7377
)
7478
}
7579

7680
/**
7781
* A non-member prefix `operator*` function for an iterator type.
7882
*/
7983
class IteratorPointerDereferenceOperator extends Operator, TaintFunction {
84+
FunctionInput iteratorInput;
85+
8086
IteratorPointerDereferenceOperator() {
8187
this.hasName("operator*") and
82-
calledWithIteratorArgument(this, 0)
88+
iteratorInput = getIteratorArgumentInput(this, 0)
8389
}
8490

8591
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
86-
input.isParameter(0) and
92+
input = iteratorInput and
8793
output.isReturnValue()
8894
}
8995
}
@@ -92,13 +98,15 @@ class IteratorPointerDereferenceOperator extends Operator, TaintFunction {
9298
* A non-member `operator++` or `operator--` function for an iterator type.
9399
*/
94100
class IteratorCrementOperator extends Operator, DataFlowFunction {
101+
FunctionInput iteratorInput;
102+
95103
IteratorCrementOperator() {
96104
this.hasName(["operator++", "operator--"]) and
97-
calledWithIteratorArgument(this, 0)
105+
iteratorInput = getIteratorArgumentInput(this, 0)
98106
}
99107

100108
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
101-
input.isParameter(0) and
109+
input = iteratorInput and
102110
output.isReturnValue()
103111
}
104112
}
@@ -107,16 +115,15 @@ class IteratorCrementOperator extends Operator, DataFlowFunction {
107115
* A non-member `operator+` function for an iterator type.
108116
*/
109117
class IteratorAddOperator extends Operator, TaintFunction {
110-
int iteratorIndex;
118+
FunctionInput iteratorInput;
111119

112120
IteratorAddOperator() {
113121
this.hasName("operator+") and
114-
iteratorIndex = [0, 1] and
115-
calledWithIteratorArgument(this, iteratorIndex)
122+
iteratorInput = getIteratorArgumentInput(this, [0, 1])
116123
}
117124

118125
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
119-
input.isParameter(iteratorIndex) and
126+
input = iteratorInput and
120127
output.isReturnValue()
121128
}
122129
}
@@ -125,14 +132,16 @@ class IteratorAddOperator extends Operator, TaintFunction {
125132
* A non-member `operator-` function that takes a pointer difference type as its second argument.
126133
*/
127134
class IteratorSubOperator extends Operator, TaintFunction {
135+
FunctionInput iteratorInput;
136+
128137
IteratorSubOperator() {
129138
this.hasName("operator-") and
130-
calledWithIteratorArgument(this, 0) and
139+
iteratorInput = getIteratorArgumentInput(this, 0) and
131140
this.getParameter(1).getUnspecifiedType() instanceof IntegralType // not an iterator difference
132141
}
133142

134143
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
135-
input.isParameter(0) and
144+
input = iteratorInput and
136145
output.isReturnValue()
137146
}
138147
}

0 commit comments

Comments
 (0)