Skip to content

Commit ab2c2ef

Browse files
committed
C#: Update isUnreachableInCall in dataflow to use Guards library.
1 parent e558c02 commit ab2c2ef

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ private module GuardsInput implements
4949
override predicate isNull() { any() }
5050
}
5151

52-
private class BooleanConstant extends ConstantExpr instanceof BoolLiteral {
53-
override boolean asBooleanValue() { result = super.getBoolValue() }
52+
private predicate boolConst(Expr e, boolean b) {
53+
e.getType() instanceof BoolType and
54+
e.getValue() = b.toString()
55+
}
56+
57+
private class BooleanConstant extends ConstantExpr {
58+
BooleanConstant() { boolConst(this, _) }
59+
60+
override boolean asBooleanValue() { boolConst(this, result) }
5461
}
5562

5663
private predicate intConst(Expr e, int i) {

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,10 +2583,10 @@ class NodeRegion instanceof ControlFlow::BasicBlock {
25832583
* Holds if the nodes in `nr` are unreachable when the call context is `call`.
25842584
*/
25852585
predicate isUnreachableInCall(NodeRegion nr, DataFlowCall call) {
2586-
exists(ExplicitParameterNode paramNode, Guard guard, ControlFlow::BooleanSuccessor bs |
2587-
viableConstantBooleanParamArg(paramNode, bs.getValue().booleanNot(), call) and
2586+
exists(ExplicitParameterNode paramNode, Guard guard, GuardValue val |
2587+
viableConstantParamArg(paramNode, val.getDualValue(), call) and
25882588
paramNode.getSsaDefinition().getARead() = guard and
2589-
guard.controlsBlock(nr, bs, _)
2589+
guard.valueControls(nr, val)
25902590
)
25912591
}
25922592

@@ -2904,33 +2904,19 @@ class CastNode extends Node {
29042904

29052905
class DataFlowExpr = Expr;
29062906

2907-
/** Holds if `e` is an expression that always has the same Boolean value `val`. */
2908-
private predicate constantBooleanExpr(Expr e, boolean val) {
2909-
e.getType() instanceof BoolType and
2910-
e.getValue() = val.toString()
2911-
or
2912-
exists(Ssa::ExplicitDefinition def, Expr src |
2913-
e = def.getARead() and
2914-
src = def.getADefinition().getSource() and
2915-
constantBooleanExpr(src, val)
2916-
)
2917-
}
2907+
/** An argument that always has the same value. */
2908+
private class ConstantArgumentNode extends ExprNode {
2909+
ConstantArgumentNode() { Guards::InternalUtil::exprHasValue(this.(ArgumentNode).asExpr(), _) }
29182910

2919-
/** An argument that always has the same Boolean value. */
2920-
private class ConstantBooleanArgumentNode extends ExprNode {
2921-
ConstantBooleanArgumentNode() { constantBooleanExpr(this.(ArgumentNode).asExpr(), _) }
2922-
2923-
/** Gets the Boolean value of this expression. */
2924-
boolean getBooleanValue() { constantBooleanExpr(this.getExpr(), result) }
2911+
/** Gets the value of this expression. */
2912+
GuardValue getValue() { Guards::InternalUtil::exprHasValue(this.getExpr(), result) }
29252913
}
29262914

29272915
pragma[noinline]
2928-
private predicate viableConstantBooleanParamArg(
2929-
ParameterNode paramNode, boolean b, DataFlowCall call
2930-
) {
2931-
exists(ConstantBooleanArgumentNode arg |
2916+
private predicate viableConstantParamArg(ParameterNode paramNode, GuardValue val, DataFlowCall call) {
2917+
exists(ConstantArgumentNode arg |
29322918
viableParamArg(call, paramNode, arg) and
2933-
b = arg.getBooleanValue()
2919+
val = arg.getValue()
29342920
)
29352921
}
29362922

0 commit comments

Comments
 (0)