Skip to content

Commit 6234b26

Browse files
committed
CPP: Make some repairs manually.
1 parent e395f52 commit 6234b26

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

cpp/ql/src/Critical/FileMayNotBeClosed.ql

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ class MinusOne extends NullValue {
2626
*/
2727
predicate mayCallFunction(Expr call, Function f) {
2828
call.(FunctionCall).getTarget() = f or
29-
call.(VariableCall).getVariable().getAnAssignedValue().getAChild*().(FunctionAccess).getTarget() =
30-
f
29+
call.(VariableCall).getVariable().getAnAssignedValue().
30+
getAChild*().(FunctionAccess).getTarget() = f
3131
}
3232

3333
predicate fopenCallOrIndirect(Expr e) {
3434
// direct fopen call
3535
fopenCall(e) and
36+
3637
// We are only interested in fopen calls that are
3738
// actually closed somehow, as FileNeverClosed
3839
// will catch those that aren't.
3940
fopenCallMayBeClosed(e)
4041
or
42+
4143
exists(ReturnStmt rtn |
4244
// indirect fopen call
4345
mayCallFunction(e, rtn.getEnclosingFunction()) and
@@ -84,6 +86,7 @@ class FOpenVariableReachability extends LocalScopeVariableReachabilityWithReassi
8486
exists(node.(AnalysedExpr).getNullSuccessor(v)) or
8587
fcloseCallOrIndirect(node, v) or
8688
assignedToFieldOrGlobal(v, node) or
89+
8790
// node may be used directly in query
8891
v.getFunction() = node.(ReturnStmt).getEnclosingFunction()
8992
}
@@ -119,10 +122,12 @@ class FOpenReachability extends LocalScopeVariableReachabilityExt {
119122
}
120123

121124
override predicate isBarrier(
122-
ControlFlowNode source, ControlFlowNode node, ControlFlowNode next, LocalScopeVariable v
123-
) {
125+
ControlFlowNode source, ControlFlowNode node, ControlFlowNode next,
126+
LocalScopeVariable v)
127+
{
124128
isSource(source, v) and
125129
next = node.getASuccessor() and
130+
126131
// the file (stored in any variable `v0`) opened at `source` is closed or
127132
// assigned to a global at node, or NULL checked on the edge node -> next.
128133
exists(LocalScopeVariable v0 | fopenVariableReaches(v0, source, node) |
@@ -167,4 +172,6 @@ where
167172
fopenVariableReaches(v, def, ret) and
168173
ret.getAChild*() = v.getAnAccess()
169174
)
170-
select def, "The file opened here may not be closed at $@.", ret, "this exit point"
175+
select
176+
def, "The file opened here may not be closed at $@.",
177+
ret, "this exit point"

cpp/ql/src/Critical/MemoryFreed.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ class FreedExpr extends PointsToExpr {
2222
override predicate interesting() { freed(this) }
2323
}
2424

25-
predicate allocMayBeFreed(Expr alloc) { isAllocationExpr(alloc) and anythingPointsTo(alloc) }
25+
predicate allocMayBeFreed(Expr alloc) {
26+
isAllocationExpr(alloc) and
27+
anythingPointsTo(alloc)
28+
}

cpp/ql/src/Critical/MemoryMayNotBeFreed.ql

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ import semmle.code.cpp.controlflow.LocalScopeVariableReachability
1818
*/
1919
predicate mayCallFunction(Expr call, Function f) {
2020
call.(FunctionCall).getTarget() = f or
21-
call.(VariableCall).getVariable().getAnAssignedValue().getAChild*().(FunctionAccess).getTarget() =
22-
f
21+
call.(VariableCall).getVariable().getAnAssignedValue().
22+
getAChild*().(FunctionAccess).getTarget() = f
2323
}
2424

2525
predicate allocCallOrIndirect(Expr e) {
2626
// direct alloc call
2727
isAllocationExpr(e) and
28+
2829
// We are only interested in alloc calls that are
2930
// actually freed somehow, as MemoryNeverFreed
3031
// will catch those that aren't.
3132
allocMayBeFreed(e)
3233
or
34+
3335
exists(ReturnStmt rtn |
3436
// indirect alloc call
3537
mayCallFunction(e, rtn.getEnclosingFunction()) and
@@ -62,6 +64,7 @@ predicate verifiedRealloc(FunctionCall reallocCall, Variable v, ControlFlowNode
6264
newV.getAnAssignedValue() = reallocCall and
6365
node.(AnalysedExpr).getNonNullSuccessor(newV) = verified and
6466
// note: this case uses naive flow logic (getAnAssignedValue).
67+
6568
// special case: if the result of the 'realloc' is assigned to the
6669
// same variable, we don't descriminate properly between the old
6770
// and the new allocation; better to not consider this a free at
@@ -113,6 +116,7 @@ class AllocVariableReachability extends LocalScopeVariableReachabilityWithReassi
113116
exists(node.(AnalysedExpr).getNullSuccessor(v)) or
114117
freeCallOrIndirect(node, v) or
115118
assignedToFieldOrGlobal(v, node) or
119+
116120
// node may be used directly in query
117121
v.getFunction() = node.(ReturnStmt).getEnclosingFunction()
118122
}
@@ -148,10 +152,12 @@ class AllocReachability extends LocalScopeVariableReachabilityExt {
148152
}
149153

150154
override predicate isBarrier(
151-
ControlFlowNode source, ControlFlowNode node, ControlFlowNode next, LocalScopeVariable v
152-
) {
155+
ControlFlowNode source, ControlFlowNode node, ControlFlowNode next,
156+
LocalScopeVariable v)
157+
{
153158
isSource(source, v) and
154159
next = node.getASuccessor() and
160+
155161
// the memory (stored in any variable `v0`) allocated at `source` is freed or
156162
// assigned to a global at node, or NULL checked on the edge node -> next.
157163
exists(LocalScopeVariable v0 | allocatedVariableReaches(v0, source, node) |
@@ -196,4 +202,6 @@ where
196202
allocatedVariableReaches(v, def, ret) and
197203
ret.getAChild*() = v.getAnAccess()
198204
)
199-
select def, "The memory allocated here may not be released at $@.", ret, "this exit point"
205+
select
206+
def, "The memory allocated here may not be released at $@.",
207+
ret, "this exit point"

cpp/ql/src/Critical/NotInitialised.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import cpp
1212

1313
// See also InitialisationNotRun.ql and GlobalUseBeforeInit.ql
14+
1415
/**
1516
* Holds if `s` defines variable `v` (conservative).
1617
*/

cpp/ql/src/Critical/OverflowDestination.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ predicate sourceSized(FunctionCall fc, Expr src) {
3333
fc.getArgument(2) = size and
3434
src = v.getAnAccess() and
3535
size.getAChild+() = v.getAnAccess() and
36+
3637
// exception: `dest` is also referenced in the size argument
3738
not exists(Variable other |
3839
dest = other.getAnAccess() and size.getAChild+() = other.getAnAccess()
3940
) and
41+
4042
// exception: `src` and `dest` are both arrays of the same type and size
4143
not exists(ArrayType srctype, ArrayType desttype |
4244
dest.getType().getUnderlyingType() = desttype and

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class BufferAccess extends ArrayExpr {
3333
staticBuffer(this.getArrayBase(), _, size) and
3434
size != 0
3535
) and
36+
3637
// exclude accesses in macro implementation of `strcmp`,
3738
// which are carefully controlled but can look dangerous.
3839
not exists(Macro m |

0 commit comments

Comments
 (0)