Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,6 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
*/
SwitchEdge getEdge() { result = edge }

/**
* Holds if this case takes control-flow from `bb1` to `bb2` when
* the case matches the scrutinee.
*/
predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
switch.getBlock() = bb1 and
this.getBasicBlock() = bb2
}

/**
* Holds if case takes control-flow from `bb1` to `bb2` when the
* case does not match the scrutinee.
*
* This predicate never holds for C/C++.
*/
predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) { none() }

/**
* Gets the scrutinee expression.
*/
Expand All @@ -188,16 +171,28 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
predicate isDefaultCase() { edge.isDefault() }

/**
* Gets the constant expression of this case.
* Gets the expression matched by this case.
*/
ConstantExpr asConstantCase() {
AstNode getAPattern() {
// Note: This only has a value if there is a unique value for the case.
// So the will not be a result when using the GCC case range extension.
// Instead, we model these using the `LogicInput_v1::rangeGuard` predicate.
result = this and exists(this.getEdge().getValue())
}
}

/**
* Holds if the case `c` takes control-flow from `bb1` to `bb2` when
* the case matches the scrutinee.
*/
predicate caseMatchEdge(Case c, BasicBlock bb1, BasicBlock bb2) {
exists(SwitchInstruction switch |
switch.getSuccessor(any(SwitchEdge e)) = c and
switch.getBlock() = bb1 and
c.getBasicBlock() = bb2
)
}

abstract private class BinExpr extends Expr instanceof BinaryInstruction {
Expr getAnOperand() { result = super.getAnInput() }
}
Expand Down
29 changes: 18 additions & 11 deletions csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl

predicate isDefaultCase() { this instanceof DefaultCase }

ConstantExpr asConstantCase() { super.getPattern() = result }

predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = true)) = bb2 and
bb1.getLastNode() = super.getPattern().getControlFlowNode()
}

predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) {
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = false)) = bb2 and
bb1.getLastNode() = super.getPattern().getControlFlowNode()
}
AstNode getAPattern() { super.getPattern() = result }
}

abstract private class BinExpr extends BinaryOperation { }
Expand Down Expand Up @@ -221,6 +211,7 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
isNull = false
)
or
// TODO: Remove this superfluous case once CFG changes for IsExpr lands.
guard =
any(PatternMatch pm |
e instanceof DereferenceableExpr and
Expand All @@ -238,6 +229,22 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
)
)
or
exists(PatternMatch pm |
guard = pm.getPattern() and
e instanceof DereferenceableExpr and
e = pm.getExpr()
|
val.isMatch(patternMatchesNull(pm.getPattern()).booleanNot()) and
isNull = false
or
exists(TypePatternExpr tpe |
// E.g. `x is string` where `x` has type `string`
typePattern(pm, tpe, tpe.getCheckedType()) and
val.isMatch(false) and
isNull = true
)
)
or
e.(DereferenceableExpr).hasNullableType() and
guard =
any(PropertyAccess pa |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@
| Guards.cs:208:17:208:17 | access to parameter o | Guards.cs:203:13:203:21 | ... != ... | Guards.cs:203:13:203:13 | access to parameter o | true |
| Guards.cs:269:13:269:14 | access to parameter o1 | Guards.cs:268:13:268:41 | call to operator == | Guards.cs:268:13:268:14 | access to parameter o1 | true |
| Guards.cs:271:13:271:14 | access to parameter o1 | Guards.cs:270:13:270:42 | call to operator == | Guards.cs:270:13:270:14 | access to parameter o1 | true |
| Guards.cs:279:17:279:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:280:28:280:28 | access to local variable a | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:281:17:281:17 | access to local variable a | true |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | true |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | true |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | false |
| Guards.cs:342:27:342:27 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | false |
| Guards.cs:343:31:343:31 | access to local variable s | Guards.cs:342:13:342:21 | ... != ... | Guards.cs:342:13:342:13 | access to local variable s | true |
| Guards.cs:349:13:349:13 | access to parameter o | Guards.cs:348:13:348:25 | ... is ... | Guards.cs:348:13:348:13 | access to parameter o | true |
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
| Guards.cs:145:20:145:20 | access to local variable s | Guards.cs:144:13:144:25 | ... is ... | Guards.cs:144:25:144:25 | access to local variable s | true |
| Guards.cs:146:16:146:16 | access to parameter o | Guards.cs:144:13:144:25 | ... is ... | Guards.cs:144:13:144:13 | access to parameter o | false |
| Guards.cs:154:24:154:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | not null |
| Guards.cs:156:24:156:24 | access to local variable a | Guards.cs:155:18:155:33 | Action<String> a | Guards.cs:155:33:155:33 | access to local variable a | match |
| Guards.cs:158:24:158:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | |
| Guards.cs:158:24:158:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | not null |
| Guards.cs:160:24:160:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | not |
Expand All @@ -178,33 +179,13 @@
| Guards.cs:269:13:269:14 | access to parameter o1 | Guards.cs:268:13:268:41 | call to operator == | Guards.cs:268:13:268:14 | access to parameter o1 | true |
| Guards.cs:271:13:271:14 | access to parameter o1 | Guards.cs:270:13:270:42 | call to operator == | Guards.cs:270:13:270:14 | access to parameter o1 | true |
| Guards.cs:279:17:279:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not null |
| Guards.cs:279:17:279:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | true |
| Guards.cs:279:17:279:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:280:28:280:28 | access to local variable a | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:281:17:281:17 | access to local variable a | true |
| Guards.cs:281:17:281:28 | call to method ToString | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:281:17:281:28 | call to method ToString | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:280:28 | Action<String> a | Guards.cs:280:28:280:28 | access to local variable a | match |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not null |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | true |
| Guards.cs:283:17:283:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | false |
| Guards.cs:283:17:283:28 | call to method ToString | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:28 | call to method ToString | true |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | null |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | true |
| Guards.cs:285:17:285:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | false |
| Guards.cs:285:17:285:28 | call to method ToString | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:28 | call to method ToString | false |
| Guards.cs:285:17:285:28 | call to method ToString | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:28 | call to method ToString | true |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not null |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | false |
| Guards.cs:287:17:287:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | false |
| Guards.cs:287:17:287:28 | call to method ToString | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:28 | call to method ToString | false |
| Guards.cs:287:17:287:28 | call to method ToString | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:28 | call to method ToString | false |
| Guards.cs:342:27:342:27 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | false |
| Guards.cs:342:27:342:27 | access to parameter b | Guards.cs:341:20:341:32 | ... ? ... : ... | Guards.cs:341:20:341:20 | access to parameter b | not null |
| Guards.cs:343:31:343:31 | access to local variable s | Guards.cs:342:13:342:13 | access to local variable s | Guards.cs:342:13:342:13 | access to local variable s | not null |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
| Guards.cs:145:20:145:20 | access to local variable s | Guards.cs:144:13:144:25 | ... is ... | Guards.cs:144:25:144:25 | access to local variable s | true |
| Guards.cs:146:16:146:16 | access to parameter o | Guards.cs:144:13:144:25 | ... is ... | Guards.cs:144:13:144:13 | access to parameter o | false |
| Guards.cs:154:24:154:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | not null |
| Guards.cs:156:24:156:24 | access to local variable a | Guards.cs:155:18:155:33 | Action<String> a | Guards.cs:155:33:155:33 | access to local variable a | match |
| Guards.cs:158:24:158:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | |
| Guards.cs:158:24:158:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | not null |
| Guards.cs:160:24:160:24 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | Guards.cs:151:17:151:17 | access to parameter o | not |
Expand All @@ -178,33 +179,13 @@
| Guards.cs:269:13:269:14 | access to parameter o1 | Guards.cs:268:13:268:41 | call to operator == | Guards.cs:268:13:268:14 | access to parameter o1 | true |
| Guards.cs:271:13:271:14 | access to parameter o1 | Guards.cs:270:13:270:42 | call to operator == | Guards.cs:270:13:270:14 | access to parameter o1 | true |
| Guards.cs:279:17:279:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not null |
| Guards.cs:279:17:279:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | true |
| Guards.cs:279:17:279:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:280:28:280:28 | access to local variable a | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:281:17:281:17 | access to local variable a | true |
| Guards.cs:281:17:281:28 | call to method ToString | Guards.cs:280:13:281:28 | ... => ... | Guards.cs:281:17:281:28 | call to method ToString | true |
| Guards.cs:281:17:281:17 | access to local variable a | Guards.cs:280:13:280:28 | Action<String> a | Guards.cs:280:28:280:28 | access to local variable a | match |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not null |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:283:17:283:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | true |
| Guards.cs:283:17:283:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | false |
| Guards.cs:283:17:283:28 | call to method ToString | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:28 | call to method ToString | true |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | null |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
| Guards.cs:285:17:285:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | true |
| Guards.cs:285:17:285:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | false |
| Guards.cs:285:17:285:28 | call to method ToString | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:28 | call to method ToString | false |
| Guards.cs:285:17:285:28 | call to method ToString | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:28 | call to method ToString | true |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | Guards.cs:276:16:276:16 | access to parameter o | not null |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:17 | access to parameter o | false |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:17 | access to parameter o | false |
| Guards.cs:287:17:287:17 | access to parameter o | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:17 | access to parameter o | false |
| Guards.cs:287:17:287:28 | call to method ToString | Guards.cs:278:13:279:28 | ... => ... | Guards.cs:279:17:279:28 | call to method ToString | false |
| Guards.cs:287:17:287:28 | call to method ToString | Guards.cs:282:13:283:28 | ... => ... | Guards.cs:283:17:283:28 | call to method ToString | false |
| Guards.cs:287:17:287:28 | call to method ToString | Guards.cs:284:13:285:28 | ... => ... | Guards.cs:285:17:285:28 | call to method ToString | false |
| Guards.cs:342:27:342:27 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | Guards.cs:341:20:341:20 | access to parameter b | false |
| Guards.cs:342:27:342:27 | access to parameter b | Guards.cs:341:20:341:32 | ... ? ... : ... | Guards.cs:341:20:341:20 | access to parameter b | not null |
| Guards.cs:343:31:343:31 | access to local variable s | Guards.cs:342:13:342:13 | access to local variable s | Guards.cs:342:13:342:13 | access to local variable s | not null |
Expand Down
Loading
Loading