Skip to content

Commit b603a3d

Browse files
authored
Merge pull request github#3259 from MathiasVP/ql-doc-fileclosed-loopbounds-memoryfreed
C++: QLDoc for FileClosed, LoopBounds and MemoryFreed
2 parents 6eac35c + 092145d commit b603a3d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

cpp/ql/src/Critical/FileClosed.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import semmle.code.cpp.pointsto.PointsTo
22

3+
/** Holds if there exists a call to a function that might close the file specified by `e`. */
34
predicate closed(Expr e) {
45
fcloseCall(_, e) or
56
exists(ExprCall c |
@@ -8,10 +9,19 @@ predicate closed(Expr e) {
89
)
910
}
1011

12+
/** An expression for which there exists a function call that might close it. */
1113
class ClosedExpr extends PointsToExpr {
1214
ClosedExpr() { closed(this) }
1315

1416
override predicate interesting() { closed(this) }
1517
}
1618

19+
/**
20+
* Holds if `fc` is a call to a function that opens a file that might be closed. For example:
21+
* ```
22+
* FILE* f = fopen("file.txt", "r");
23+
* ...
24+
* fclose(f);
25+
* ```
26+
*/
1727
predicate fopenCallMayBeClosed(FunctionCall fc) { fopenCall(fc) and anythingPointsTo(fc) }

cpp/ql/src/Critical/LoopBounds.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
import cpp
44

5+
/**
6+
* An assignment to a variable with the value `0`. For example:
7+
* ```
8+
* int x;
9+
* x = 0;
10+
* ```
11+
* but not:
12+
* ```
13+
* int x = 0;
14+
* ```
15+
*/
516
class ZeroAssignment extends AssignExpr {
617
ZeroAssignment() {
718
this.getAnOperand() instanceof VariableAccess and
819
this.getAnOperand() instanceof Zero
920
}
1021

22+
/** Gets a variable that is assigned the value `0`. */
1123
Variable assignedVariable() { result.getAnAccess() = this.getAnOperand() }
1224
}
1325

cpp/ql/src/Critical/MemoryFreed.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ private predicate freed(Expr e) {
99
)
1010
}
1111

12+
/** An expression that might be deallocated. */
1213
class FreedExpr extends PointsToExpr {
1314
FreedExpr() { freed(this) }
1415

1516
override predicate interesting() { freed(this) }
1617
}
1718

19+
/**
20+
* An allocation expression that might be deallocated. For example:
21+
* ```
22+
* int* p = new int;
23+
* ...
24+
* delete p;
25+
* ```
26+
*/
1827
predicate allocMayBeFreed(AllocationExpr alloc) { anythingPointsTo(alloc) }

0 commit comments

Comments
 (0)