C++: QLDoc for FileClosed, LoopBounds and MemoryFreed

This commit is contained in:
Mathias Vorreiter Pedersen
2020-04-14 10:20:35 +02:00
parent 7c5c9ea8ea
commit d8dcbe3cbd
3 changed files with 30 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import semmle.code.cpp.pointsto.PointsTo import semmle.code.cpp.pointsto.PointsTo
/** Holds if there exists a call to a function that might close the file specified by `e`. */
predicate closed(Expr e) { predicate closed(Expr e) {
fcloseCall(_, e) or fcloseCall(_, e) or
exists(ExprCall c | exists(ExprCall c |
@@ -8,10 +9,19 @@ predicate closed(Expr e) {
) )
} }
/** An expression for which there exists a function call that might close it. */
class ClosedExpr extends PointsToExpr { class ClosedExpr extends PointsToExpr {
ClosedExpr() { closed(this) } ClosedExpr() { closed(this) }
override predicate interesting() { closed(this) } override predicate interesting() { closed(this) }
} }
/**
* Holds if `fc` is a call to function that opens a file which might be closed. For example:
* ```
* FILE* f = fopen("file.txt", "r");
* ...
* fclose(f);
* ```
*/
predicate fopenCallMayBeClosed(FunctionCall fc) { fopenCall(fc) and anythingPointsTo(fc) } predicate fopenCallMayBeClosed(FunctionCall fc) { fopenCall(fc) and anythingPointsTo(fc) }

View File

@@ -2,12 +2,23 @@
import cpp import cpp
/** An assignment to a variable with the value `0`. For example:
* ```
* int x;
* x = 0;
* ```
* but not:
* ```
* int x = 0;
* ```
*/
class ZeroAssignment extends AssignExpr { class ZeroAssignment extends AssignExpr {
ZeroAssignment() { ZeroAssignment() {
this.getAnOperand() instanceof VariableAccess and this.getAnOperand() instanceof VariableAccess and
this.getAnOperand() instanceof Zero this.getAnOperand() instanceof Zero
} }
/** Gets a variable that is assigned the value `0`. */
Variable assignedVariable() { result.getAnAccess() = this.getAnOperand() } Variable assignedVariable() { result.getAnAccess() = this.getAnOperand() }
} }

View File

@@ -9,10 +9,19 @@ private predicate freed(Expr e) {
) )
} }
/** An expression that might be deallocated. */
class FreedExpr extends PointsToExpr { class FreedExpr extends PointsToExpr {
FreedExpr() { freed(this) } FreedExpr() { freed(this) }
override predicate interesting() { freed(this) } override predicate interesting() { freed(this) }
} }
/**
* An allocation expression that might be deallocated. For example:
* ```
* int* p = new int;
* ...
* delete p;
* ```
*/
predicate allocMayBeFreed(AllocationExpr alloc) { anythingPointsTo(alloc) } predicate allocMayBeFreed(AllocationExpr alloc) { anythingPointsTo(alloc) }