Skip to content

Commit c5fe049

Browse files
committed
Add stream insertion and extraction operators
1 parent d9b41d2 commit c5fe049

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cpp/common/src/codingstandards/cpp/Operator.qll

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cpp
22
import Expr
3+
private import semmle.code.cpp.security.FileWrite
34

45
/**
56
* any assignment operator that also reads from the access
@@ -264,3 +265,51 @@ class UserOverloadedOperator extends Function {
264265
not this.isCompilerGenerated()
265266
}
266267
}
268+
269+
/**
270+
* A `std::basic_istream` class, or something that can be used
271+
* as one. Based on the BasicOStreamClass.
272+
*/
273+
private class BasicIStreamClass extends Type {
274+
BasicIStreamClass() {
275+
this.(Class).getName().matches("basic\\_istream%")
276+
or
277+
this.getUnspecifiedType() instanceof BasicIStreamClass
278+
or
279+
this.(Class).getABaseClass() instanceof BasicIStreamClass
280+
or
281+
this.(ReferenceType).getBaseType() instanceof BasicIStreamClass
282+
}
283+
}
284+
285+
/** An implementation of a stream insertion operator. */
286+
class StreamInsertionOperator extends Function {
287+
StreamInsertionOperator() {
288+
this.hasName("operator<<") and
289+
(
290+
if this.isMember()
291+
then this.getNumberOfParameters() = 1
292+
else (
293+
this.getNumberOfParameters() = 2 and
294+
this.getParameter(0).getType() instanceof BasicOStreamClass
295+
)
296+
) and
297+
this.getType() instanceof BasicOStreamClass
298+
}
299+
}
300+
301+
/** An implementation of a stream extraction operator. */
302+
class StreamExtractionOperator extends Function {
303+
StreamExtractionOperator() {
304+
this.hasName("operator>>") and
305+
(
306+
if this.isMember()
307+
then this.getNumberOfParameters() = 1
308+
else (
309+
this.getNumberOfParameters() = 2 and
310+
this.getParameter(0).getType() instanceof BasicIStreamClass
311+
)
312+
) and
313+
this.getType() instanceof BasicIStreamClass
314+
}
315+
}

0 commit comments

Comments
 (0)