mirror of
https://github.com/github/codeql.git
synced 2026-04-25 16:55:19 +02:00
Fix guard basic block for switch cases
This commit is contained in:
@@ -450,6 +450,20 @@ class SwitchCase extends Stmt, @case {
|
||||
result = this.getSwitch().getExpr() or result = this.getSwitchExpr().getExpr()
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `i`th case in this case's switch block.
|
||||
*/
|
||||
SwitchCase getSiblingCase(int i) {
|
||||
result = this.getSwitch().getCase(i) or result = this.getSwitchExpr().getCase(i)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this case's ordinal in its switch block.
|
||||
*/
|
||||
int getCaseIndex() {
|
||||
this = this.getSwitch().getCase(result) or this = this.getSwitchExpr().getCase(result)
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if this `case` is a switch labeled rule of the form `... -> ...`.
|
||||
*/
|
||||
|
||||
@@ -103,11 +103,27 @@ class Guard extends ExprParent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the basic block containing this guard or the basic block containing
|
||||
* the switch expression if the guard is a switch case.
|
||||
* Gets the basic block containing this guard or the basic block that tests the
|
||||
* applicability of this switch case -- for a pattern case this is the case statement
|
||||
* itself; for a non-pattern case this is the most recent pattern case or the top of
|
||||
* the switch block if there is none.
|
||||
*/
|
||||
BasicBlock getBasicBlock() {
|
||||
result = this.(Expr).getBasicBlock() or
|
||||
// Not a switch case
|
||||
result = this.(Expr).getBasicBlock()
|
||||
or
|
||||
// Return the closest pattern case statement before this one, including this one.
|
||||
result =
|
||||
max(int i, PatternCase c |
|
||||
c = this.(SwitchCase).getSiblingCase(i) and i <= this.(SwitchCase).getCaseIndex()
|
||||
|
|
||||
c order by i
|
||||
).getBasicBlock()
|
||||
or
|
||||
// Not a pattern case and no preceding pattern case -- return the top of the switch block.
|
||||
not exists(PatternCase c, int i |
|
||||
c = this.(SwitchCase).getSiblingCase(i) and i <= this.(SwitchCase).getCaseIndex()
|
||||
) and
|
||||
result = this.(SwitchCase).getSelectorExpr().getBasicBlock()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user