Skip to content

Commit ef8c9be

Browse files
committed
Reuse and extend existing modelling
1 parent a10476f commit ef8c9be

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

cpp/cert/src/rules/FIO51-CPP/CloseFilesWhenTheyAreNoLongerNeeded.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ predicate filebufAccess(ControlFlowNode node, FileStreamSource fss) {
3838
//insertion or extraction operator calls
3939
node.(InsertionOperatorCall).getFStream() = fss.getAUse() or
4040
node.(ExtractionOperatorCall).getFStream() = fss.getAUse() or
41-
//methods inherited from istream or ostream
42-
node.(IOStreamFunctionCall).getFStream() = fss.getAUse()
41+
// Methods inherited from istream or ostream that access the file stream.
42+
// Exclude is_open as it is not a filebuf access
43+
any(IOStreamFunctionCall call | node = call and not call.getTarget().hasName("is_open")).getFStream() = fss.getAUse()
4344
}
4445

4546
/**

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cpp
22
import Expr
33
private import semmle.code.cpp.security.FileWrite
4+
private import codingstandards.cpp.standardlibrary.FileStreams
45

56
/**
67
* any assignment operator that also reads from the access
@@ -268,22 +269,6 @@ class UserOverloadedOperator extends Function {
268269
}
269270
}
270271

271-
/**
272-
* A `std::basic_istream` class, or something that can be used
273-
* as one. Based on the BasicOStreamClass.
274-
*/
275-
private class BasicIStreamClass extends Type {
276-
BasicIStreamClass() {
277-
this.(Class).getName().matches("basic\\_istream%")
278-
or
279-
this.getUnspecifiedType() instanceof BasicIStreamClass
280-
or
281-
this.(Class).getABaseClass() instanceof BasicIStreamClass
282-
or
283-
this.(ReferenceType).getBaseType() instanceof BasicIStreamClass
284-
}
285-
}
286-
287272
/** An implementation of a stream insertion operator. */
288273
class StreamInsertionOperator extends Function {
289274
StreamInsertionOperator() {
@@ -309,9 +294,9 @@ class StreamExtractionOperator extends Function {
309294
then this.getNumberOfParameters() = 1
310295
else (
311296
this.getNumberOfParameters() = 2 and
312-
this.getParameter(0).getType() instanceof BasicIStreamClass
297+
this.getParameter(0).getType() instanceof IStream
313298
)
314299
) and
315-
this.getType() instanceof BasicIStreamClass
300+
this.getType() instanceof IStream
316301
}
317302
}

cpp/common/src/codingstandards/cpp/standardlibrary/FileStreams.qll

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import cpp
1313
import codingstandards.cpp.dataflow.DataFlow
1414
import codingstandards.cpp.dataflow.TaintTracking
15+
private import codingstandards.cpp.Operator
1516

1617
/**
1718
* A `basic_fstream` like `std::fstream`
@@ -23,15 +24,31 @@ class FileStream extends ClassTemplateInstantiation {
2324
/**
2425
* A `basic_istream` like `std::istream`
2526
*/
26-
class IStream extends ClassTemplateInstantiation {
27-
IStream() { this.getTemplate().hasQualifiedName("std", "basic_istream") }
27+
class IStream extends Type {
28+
IStream() {
29+
this.(Class).getQualifiedName().matches("std::basic\\_istream%")
30+
or
31+
this.getUnspecifiedType() instanceof IStream
32+
or
33+
this.(Class).getABaseClass() instanceof IStream
34+
or
35+
this.(ReferenceType).getBaseType() instanceof IStream
36+
}
2837
}
2938

3039
/**
3140
* A `basic_ostream` like `std::ostream`
3241
*/
33-
class OStream extends ClassTemplateInstantiation {
34-
OStream() { this.getTemplate().hasQualifiedName("std", "basic_ostream") }
42+
class OStream extends Type {
43+
OStream() {
44+
this.(Class).getQualifiedName().matches("std::basic\\_ostream%")
45+
or
46+
this.getUnspecifiedType() instanceof OStream
47+
or
48+
this.(Class).getABaseClass() instanceof OStream
49+
or
50+
this.(ReferenceType).getBaseType() instanceof OStream
51+
}
3552
}
3653

3754
/**
@@ -53,7 +70,7 @@ predicate sameStreamSource(FileStreamFunctionCall a, FileStreamFunctionCall b) {
5370
* Insertion `operator<<` and Extraction `operator>>` operators.
5471
*/
5572
class InsertionOperatorCall extends FileStreamFunctionCall {
56-
InsertionOperatorCall() { this.getTarget().(Operator).hasQualifiedName("std", "operator<<") }
73+
InsertionOperatorCall() { this.getTarget() instanceof StreamInsertionOperator }
5774

5875
override Expr getFStream() {
5976
result = this.getQualifier()
@@ -63,7 +80,7 @@ class InsertionOperatorCall extends FileStreamFunctionCall {
6380
}
6481

6582
class ExtractionOperatorCall extends FileStreamFunctionCall {
66-
ExtractionOperatorCall() { this.getTarget().(Operator).hasQualifiedName("std", "operator>>") }
83+
ExtractionOperatorCall() { this.getTarget() instanceof StreamExtractionOperator }
6784

6885
override Expr getFStream() {
6986
result = this.getQualifier()

0 commit comments

Comments
 (0)