Skip to content

Commit dae65f6

Browse files
authored
Merge pull request github#5150 from Marcono1234/marcono1234/conditional-expr-branch
Java: Add ConditionalExpr.getBranchExpr(boolean)
2 parents c4cca83 + 9e2812c commit dae65f6

File tree

17 files changed

+61
-80
lines changed

17 files changed

+61
-80
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/ModulusAnalysis.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ predicate exprModulus(Expr e, Bound b, int val, int mod) {
268268
private predicate condExprBranchModulus(
269269
ConditionalExpr cond, boolean branch, Bound b, int val, int mod
270270
) {
271-
exprModulus(cond.getTrueExpr(), b, val, mod) and branch = true
272-
or
273-
exprModulus(cond.getFalseExpr(), b, val, mod) and branch = false
271+
exprModulus(cond.getBranchExpr(branch), b, val, mod)
274272
}
275273

276274
private predicate addModulus(Expr add, boolean isLeft, Bound b, int val, int mod) {

csharp/ql/src/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,15 @@ module ExprNode {
415415

416416
/** Gets the "else" expression of this conditional expression. */
417417
ExprNode getFalseExpr() { hasChild(e, e.getElse(), this, result) }
418+
419+
/**
420+
* If `branch` is `true` gets the "then" expression, if `false` gets the
421+
* "else" expression of this conditional expression.
422+
*/
423+
ExprNode getBranchExpr(boolean branch) {
424+
branch = true and result = getTrueExpr()
425+
or
426+
branch = false and result = getFalseExpr()
427+
}
418428
}
419429
}

java/ql/src/Language Abuse/MissedTernaryOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java
1515

1616
predicate complicatedBranch(Stmt branch) {
17-
exists(ConditionalExpr ce | ce.getParent*() = branch) or
17+
any(ConditionalExpr ce).getParent*() = branch or
1818
count(MethodAccess a | a.getParent*() = branch) > 1
1919
}
2020

java/ql/src/Language Abuse/UselessUpcast.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ predicate usefulUpcast(CastExpr e) {
3636
)
3737
or
3838
// Upcasts that are performed on an operand of a ternary expression.
39-
exists(ConditionalExpr ce | e = ce.getTrueExpr() or e = ce.getFalseExpr())
39+
e = any(ConditionalExpr ce).getABranchExpr()
4040
or
4141
// Upcasts to raw types.
4242
e.getType() instanceof RawType

java/ql/src/Likely Bugs/Arithmetic/CondExprTypes.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ class CharType extends PrimitiveType {
1616
CharType() { this.hasName("char") }
1717
}
1818

19-
private Type getABranchType(ConditionalExpr ce) {
20-
result = ce.getTrueExpr().getType() or
21-
result = ce.getFalseExpr().getType()
22-
}
19+
private Type getABranchType(ConditionalExpr ce) { result = ce.getABranchExpr().getType() }
2320

2421
from ConditionalExpr ce
2522
where

java/ql/src/Likely Bugs/Comparison/NoAssignInBooleanExprs.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import semmle.code.java.Statement
1717
/** An expression that is used as a condition. */
1818
class BooleanExpr extends Expr {
1919
BooleanExpr() {
20-
exists(ConditionalStmt s | s.getCondition() = this) or
21-
exists(ConditionalExpr s | s.getCondition() = this)
20+
this = any(ConditionalStmt s).getCondition() or
21+
this = any(ConditionalExpr s).getCondition()
2222
}
2323
}
2424

java/ql/src/Likely Bugs/Resource Leaks/CloseType.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private predicate flowsInto(Expr e, Variable v) {
1111
or
1212
exists(CastExpr c | flowsInto(c, v) | e = c.getExpr())
1313
or
14-
exists(ConditionalExpr c | flowsInto(c, v) | e = c.getTrueExpr() or e = c.getFalseExpr())
14+
exists(ConditionalExpr c | flowsInto(c, v) | e = c.getABranchExpr())
1515
}
1616

1717
/**

java/ql/src/Security/CWE/CWE-190/ArithmeticCommon.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ predicate upcastToWiderType(Expr e) {
148148
or
149149
exists(Parameter p | p.getAnArgument() = e and t2 = p.getType())
150150
or
151-
exists(ConditionalExpr cond | cond.getTrueExpr() = e or cond.getFalseExpr() = e |
152-
t2 = cond.getType()
153-
)
151+
exists(ConditionalExpr cond | cond.getABranchExpr() = e | t2 = cond.getType())
154152
)
155153
}
156154

java/ql/src/Violations of Best Practice/legacy/AutoBoxing.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ predicate unboxed(BoxedExpr e) {
4545
or
4646
flowTarget(e).getType() instanceof PrimitiveType
4747
or
48-
exists(ConditionalExpr cond | cond instanceof PrimitiveExpr |
49-
cond.getTrueExpr() = e or cond.getFalseExpr() = e
50-
)
48+
exists(ConditionalExpr cond | cond instanceof PrimitiveExpr | cond.getABranchExpr() = e)
5149
}
5250

5351
/**

java/ql/src/semmle/code/java/ControlFlowGraph.qll

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private module ControlFlowGraphImpl {
293293
exists(ConditionalExpr condexpr |
294294
condexpr.getCondition() = b
295295
or
296-
(condexpr.getTrueExpr() = b or condexpr.getFalseExpr() = b) and
296+
condexpr.getABranchExpr() = b and
297297
inBooleanContext(condexpr)
298298
)
299299
or
@@ -706,8 +706,7 @@ private module ControlFlowGraphImpl {
706706
or
707707
// The last node of a `ConditionalExpr` is in either of its branches.
708708
exists(ConditionalExpr condexpr | condexpr = n |
709-
last(condexpr.getFalseExpr(), last, completion) or
710-
last(condexpr.getTrueExpr(), last, completion)
709+
last(condexpr.getABranchExpr(), last, completion)
711710
)
712711
or
713712
exists(InstanceOfExpr ioe | ioe.isPattern() and ioe = n |
@@ -915,14 +914,10 @@ private module ControlFlowGraphImpl {
915914
)
916915
or
917916
// Control flows to the corresponding branch depending on the boolean completion of the condition.
918-
exists(ConditionalExpr e |
917+
exists(ConditionalExpr e, boolean branch |
919918
last(e.getCondition(), n, completion) and
920-
completion = BooleanCompletion(true, _) and
921-
result = first(e.getTrueExpr())
922-
or
923-
last(e.getCondition(), n, completion) and
924-
completion = BooleanCompletion(false, _) and
925-
result = first(e.getFalseExpr())
919+
completion = BooleanCompletion(branch, _) and
920+
result = first(e.getBranchExpr(branch))
926921
)
927922
or
928923
exists(InstanceOfExpr ioe | ioe.isPattern() |

0 commit comments

Comments
 (0)