Skip to content

Commit 140575e

Browse files
committed
C++: Use StackVariable where SSA/def-use are used
These changes should not affect semantics since these uses of `LocalScopeVariable` were already constrained to stack variables by their use of SSA or def-use.
1 parent 6f9ec04 commit 140575e

File tree

14 files changed

+41
-45
lines changed

14 files changed

+41
-45
lines changed

cpp/ql/src/Critical/DeadCodeCondition.ql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ predicate testAndBranch(Expr e, Stmt branch) {
2222
)
2323
}
2424

25-
predicate choice(LocalScopeVariable v, Stmt branch, string value) {
25+
predicate choice(StackVariable v, Stmt branch, string value) {
2626
exists(AnalysedExpr e |
2727
testAndBranch(e, branch) and
2828
(
@@ -33,7 +33,7 @@ predicate choice(LocalScopeVariable v, Stmt branch, string value) {
3333
)
3434
}
3535

36-
predicate guarded(LocalScopeVariable v, Stmt loopstart, AnalysedExpr child) {
36+
predicate guarded(StackVariable v, Stmt loopstart, AnalysedExpr child) {
3737
choice(v, loopstart, _) and
3838
loopstart.getChildStmt*() = child.getEnclosingStmt() and
3939
(definition(v, child) or exists(child.getNullSuccessor(v)))
@@ -47,9 +47,7 @@ predicate addressLeak(Variable v, Stmt leak) {
4747
)
4848
}
4949

50-
from
51-
LocalScopeVariable v, Stmt branch, AnalysedExpr cond, string context, string test,
52-
string testresult
50+
from StackVariable v, Stmt branch, AnalysedExpr cond, string context, string test, string testresult
5351
where
5452
choice(v, branch, context) and
5553
forall(ControlFlowNode def | definition(v, def) and definitionReaches(def, cond) |

cpp/ql/src/Critical/DescriptorMayNotBeClosed.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ predicate closeCall(FunctionCall fc, Variable v) {
2323
)
2424
}
2525

26-
predicate openDefinition(LocalScopeVariable v, ControlFlowNode def) {
26+
predicate openDefinition(StackVariable v, ControlFlowNode def) {
2727
exists(Expr expr | exprDefinition(v, def, expr) and allocateDescriptorCall(expr))
2828
}
2929

3030
predicate openReaches(ControlFlowNode def, ControlFlowNode node) {
31-
exists(LocalScopeVariable v | openDefinition(v, def) and node = def.getASuccessor())
31+
exists(StackVariable v | openDefinition(v, def) and node = def.getASuccessor())
3232
or
33-
exists(LocalScopeVariable v, ControlFlowNode mid |
33+
exists(StackVariable v, ControlFlowNode mid |
3434
openDefinition(v, def) and
3535
openReaches(def, mid) and
3636
not errorSuccessor(v, mid) and
@@ -40,15 +40,15 @@ predicate openReaches(ControlFlowNode def, ControlFlowNode node) {
4040
)
4141
}
4242

43-
predicate assignedToFieldOrGlobal(LocalScopeVariable v, Assignment assign) {
43+
predicate assignedToFieldOrGlobal(StackVariable v, Assignment assign) {
4444
exists(Variable external |
4545
assign.getRValue() = v.getAnAccess() and
4646
assign.getLValue().(VariableAccess).getTarget() = external and
4747
(external instanceof Field or external instanceof GlobalVariable)
4848
)
4949
}
5050

51-
from LocalScopeVariable v, ControlFlowNode def, ReturnStmt ret
51+
from StackVariable v, ControlFlowNode def, ReturnStmt ret
5252
where
5353
openDefinition(v, def) and
5454
openReaches(def, ret) and

cpp/ql/src/Critical/InconsistentNullnessTesting.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import cpp
1313

14-
from LocalScopeVariable v, ControlFlowNode def, VariableAccess checked, VariableAccess unchecked
14+
from StackVariable v, ControlFlowNode def, VariableAccess checked, VariableAccess unchecked
1515
where
1616
checked = v.getAnAccess() and
1717
dereferenced(checked) and

cpp/ql/src/Critical/LateNegativeTest.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import cpp
1515

16-
predicate negativeCheck(LocalScopeVariable v, ComparisonOperation op) {
16+
predicate negativeCheck(StackVariable v, ComparisonOperation op) {
1717
exists(int varindex, string constant, Literal lit |
1818
op.getChild(varindex) = v.getAnAccess() and
1919
op.getChild(1 - varindex) = lit and
@@ -38,7 +38,7 @@ predicate negativeCheck(LocalScopeVariable v, ComparisonOperation op) {
3838
)
3939
}
4040

41-
from LocalScopeVariable v, ArrayExpr dangerous, Expr check
41+
from StackVariable v, ArrayExpr dangerous, Expr check
4242
where
4343
useUsePair(v, dangerous.getArrayOffset(), check.getAChild()) and
4444
negativeCheck(v, check) and

cpp/ql/src/Critical/MissingNegativityTest.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class FunctionWithNegativeReturn extends Function {
4343
predicate dangerousUse(IntegralReturnValue val, Expr use) {
4444
exists(ArrayExpr ae | ae.getArrayOffset() = val and use = val)
4545
or
46-
exists(LocalScopeVariable v, ControlFlowNode def, ArrayExpr ae |
46+
exists(StackVariable v, ControlFlowNode def, ArrayExpr ae |
4747
exprDefinition(v, def, val) and
4848
use = ae.getArrayOffset() and
4949
not boundsChecked(v, use) and
@@ -54,7 +54,7 @@ predicate dangerousUse(IntegralReturnValue val, Expr use) {
5454
val = use and
5555
use.getType().getUnderlyingType() instanceof PointerType
5656
or
57-
exists(LocalScopeVariable v, ControlFlowNode def, AddExpr add |
57+
exists(StackVariable v, ControlFlowNode def, AddExpr add |
5858
exprDefinition(v, def, val) and
5959
definitionUsePair(v, def, use) and
6060
add.getAnOperand() = use and

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ predicate allocExprOrIndirect(Expr alloc, string kind) {
6060
pragma[nomagic]
6161
private predicate allocReachesVariable(Variable v, Expr alloc, string kind) {
6262
exists(Expr mid |
63-
not v instanceof LocalScopeVariable and
63+
not v instanceof StackVariable and
6464
v.getAnAssignedValue() = mid and
6565
allocReaches0(mid, alloc, kind)
6666
)
@@ -76,7 +76,7 @@ private predicate allocReaches0(Expr e, Expr alloc, string kind) {
7676
allocExprOrIndirect(alloc, kind) and
7777
e = alloc
7878
or
79-
exists(SsaDefinition def, LocalScopeVariable v |
79+
exists(SsaDefinition def, StackVariable v |
8080
// alloc via SSA
8181
allocReaches0(def.getAnUltimateDefiningValue(v), alloc, kind) and
8282
e = def.getAUse(v)

cpp/ql/src/Critical/OverflowCalculated.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MallocCall extends FunctionCall {
1818
Expr getAllocatedSize() {
1919
if this.getArgument(0) instanceof VariableAccess
2020
then
21-
exists(LocalScopeVariable v, ControlFlowNode def |
21+
exists(StackVariable v, ControlFlowNode def |
2222
definitionUsePair(v, def, this.getArgument(0)) and
2323
exprDefinition(v, def, result)
2424
)

cpp/ql/src/Security/CWE/CWE-129/ImproperArrayIndexValidation.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1616
import semmle.code.cpp.security.TaintTracking
1717

1818
predicate hasUpperBound(VariableAccess offsetExpr) {
19-
exists(BasicBlock controlled, LocalScopeVariable offsetVar, SsaDefinition def |
19+
exists(BasicBlock controlled, StackVariable offsetVar, SsaDefinition def |
2020
controlled.contains(offsetExpr) and
2121
linearBoundControls(controlled, def, offsetVar) and
2222
offsetExpr = def.getAUse(offsetVar)
2323
)
2424
}
2525

2626
pragma[noinline]
27-
predicate linearBoundControls(BasicBlock controlled, SsaDefinition def, LocalScopeVariable offsetVar) {
27+
predicate linearBoundControls(BasicBlock controlled, SsaDefinition def, StackVariable offsetVar) {
2828
exists(GuardCondition guard, boolean branch |
2929
guard.controls(controlled, branch) and
3030
cmpWithLinearBound(guard, def.getAUse(offsetVar), Lesser(), branch)

cpp/ql/src/Security/CWE/CWE-131/NoSpaceForZeroTerminator.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MallocCall extends FunctionCall {
2424
Expr getAllocatedSize() {
2525
if this.getArgument(0) instanceof VariableAccess
2626
then
27-
exists(LocalScopeVariable v, ControlFlowNode def |
27+
exists(StackVariable v, ControlFlowNode def |
2828
definitionUsePair(v, def, this.getArgument(0)) and
2929
exprDefinition(v, def, result)
3030
)

cpp/ql/src/Security/CWE/CWE-367/TOCTOUFilesystemRace.ql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ FunctionCall stat(Expr path, Expr buf) {
8282
predicate referenceTo(Expr source, Expr use) {
8383
source = use
8484
or
85-
exists(SsaDefinition def, LocalScopeVariable v |
85+
exists(SsaDefinition def, StackVariable v |
8686
def.getAnUltimateDefiningValue(v) = source and def.getAUse(v) = use
8787
)
8888
}
@@ -109,9 +109,7 @@ where
109109
)
110110
) and
111111
// checkUse and opUse refer to the same SSA variable
112-
exists(SsaDefinition def, LocalScopeVariable v |
113-
def.getAUse(v) = checkUse and def.getAUse(v) = opUse
114-
) and
112+
exists(SsaDefinition def, StackVariable v | def.getAUse(v) = checkUse and def.getAUse(v) = opUse) and
115113
// opUse looks like an operation on a filename
116114
fc = filenameOperation(opUse) and
117115
// the return value of check is used (possibly with one step of

0 commit comments

Comments
 (0)