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
4 changes: 4 additions & 0 deletions actions/ql/lib/codeql/actions/controlflow/internal/Cfg.qll
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ module CfgImpl {
ContinueStmt() { none() }
}

class FallthroughStmt extends Stmt {
FallthroughStmt() { none() }
}

class GotoStmt extends Stmt {
GotoStmt() { none() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ module Ast implements AstSig<Location> {

class ContinueStmt = CS::ContinueStmt;

class FallthroughStmt extends Stmt {
FallthroughStmt() { none() }
}

class GotoStmt = CS::GotoStmt;

class ReturnStmt = CS::ReturnStmt;
Expand Down
9 changes: 3 additions & 6 deletions go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ module CfgImpl {

class ContinueStmt = Go::ContinueStmt;

class FallthroughStmt = Go::FallthroughStmt;

class GotoStmt = Go::GotoStmt;

class ReturnStmt = Go::ReturnStmt;
Expand Down Expand Up @@ -324,12 +326,7 @@ module CfgImpl {

AstNode getSwitchInit(Switch switch) { result = switch.(Go::SwitchStmt).getInit() }

predicate fallsThrough(Case c) {
// Go has no implicit fall-through between case clauses; an explicit
// `fallthrough` statement is required.
c.(Go::CaseClause).getStmt(max(int i | exists(c.(Go::CaseClause).getStmt(i)))) instanceof
Go::FallthroughStmt
}
predicate fallsThrough(Case c) { none() }

/**
* Holds if `s` is the flattened body element at position (`caseIdx`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,83 @@
| exprs.go:94:15:94:15 | y | exprs.go:94:15:94:15 | After y [false] |
| exprs.go:94:15:94:15 | y | exprs.go:94:15:94:15 | After y [true] |
| exprs.go:94:21:94:21 | z | exprs.go:94:9:94:21 | After ...\|\|... |
| fallthrough.go:0:0:0:0 | After fallthrough.go | fallthrough.go:0:0:0:0 | Normal Exit |
| fallthrough.go:0:0:0:0 | Entry | fallthrough.go:0:0:0:0 | fallthrough.go |
| fallthrough.go:0:0:0:0 | Normal Exit | fallthrough.go:0:0:0:0 | Exit |
| fallthrough.go:0:0:0:0 | fallthrough.go | fallthrough.go:3:1:12:1 | function declaration |
| fallthrough.go:3:1:12:1 | Entry | fallthrough.go:3:29:3:29 | x |
| fallthrough.go:3:1:12:1 | Exceptional Exit | fallthrough.go:3:1:12:1 | Exit |
| fallthrough.go:3:1:12:1 | Normal Exit | fallthrough.go:3:1:12:1 | Exit |
| fallthrough.go:3:1:12:1 | function declaration | fallthrough.go:14:1:21:1 | function declaration |
| fallthrough.go:3:29:3:29 | x | fallthrough.go:3:36:12:1 | block statement |
| fallthrough.go:3:36:12:1 | After block statement | fallthrough.go:3:1:12:1 | Normal Exit |
| fallthrough.go:3:36:12:1 | block statement | fallthrough.go:4:2:10:2 | expression-switch statement |
| fallthrough.go:4:2:10:2 | After expression-switch statement | fallthrough.go:11:2:11:13 | expression statement |
| fallthrough.go:4:2:10:2 | expression-switch statement | fallthrough.go:4:9:4:9 | x |
| fallthrough.go:4:9:4:9 | x | fallthrough.go:5:2:6:13 | case clause |
| fallthrough.go:5:2:6:13 | After case clause [match] | fallthrough.go:6:3:6:13 | Before fallthrough statement |
| fallthrough.go:5:2:6:13 | After case clause [no-match] | fallthrough.go:7:2:7:8 | case clause |
| fallthrough.go:5:2:6:13 | case clause | fallthrough.go:5:7:5:7 | 0 |
| fallthrough.go:5:7:5:7 | 0 | fallthrough.go:5:7:5:7 | After 0 [match] |
| fallthrough.go:5:7:5:7 | 0 | fallthrough.go:5:7:5:7 | After 0 [no-match] |
| fallthrough.go:5:7:5:7 | After 0 [match] | fallthrough.go:5:2:6:13 | After case clause [match] |
| fallthrough.go:5:7:5:7 | After 0 [no-match] | fallthrough.go:5:2:6:13 | After case clause [no-match] |
| fallthrough.go:6:3:6:13 | Before fallthrough statement | fallthrough.go:6:3:6:13 | fallthrough statement |
| fallthrough.go:6:3:6:13 | fallthrough statement | fallthrough.go:4:2:10:2 | After expression-switch statement |
| fallthrough.go:7:2:7:8 | After case clause [match] | fallthrough.go:4:2:10:2 | After expression-switch statement |
| fallthrough.go:7:2:7:8 | After case clause [no-match] | fallthrough.go:8:2:9:13 | case clause |
| fallthrough.go:7:2:7:8 | case clause | fallthrough.go:7:7:7:7 | 1 |
| fallthrough.go:7:7:7:7 | 1 | fallthrough.go:7:7:7:7 | After 1 [match] |
| fallthrough.go:7:7:7:7 | 1 | fallthrough.go:7:7:7:7 | After 1 [no-match] |
| fallthrough.go:7:7:7:7 | After 1 [match] | fallthrough.go:7:2:7:8 | After case clause [match] |
| fallthrough.go:7:7:7:7 | After 1 [no-match] | fallthrough.go:7:2:7:8 | After case clause [no-match] |
| fallthrough.go:8:2:9:13 | After case clause [match] | fallthrough.go:9:3:9:13 | expression statement |
| fallthrough.go:8:2:9:13 | case clause | fallthrough.go:8:2:9:13 | After case clause [match] |
| fallthrough.go:9:3:9:7 | test5 | fallthrough.go:9:9:9:12 | true |
| fallthrough.go:9:3:9:13 | After call to test5 | fallthrough.go:9:3:9:13 | After expression statement |
| fallthrough.go:9:3:9:13 | After expression statement | fallthrough.go:4:2:10:2 | After expression-switch statement |
| fallthrough.go:9:3:9:13 | Before call to test5 | fallthrough.go:9:3:9:7 | test5 |
| fallthrough.go:9:3:9:13 | call to test5 | fallthrough.go:3:1:12:1 | Exceptional Exit |
| fallthrough.go:9:3:9:13 | call to test5 | fallthrough.go:9:3:9:13 | After call to test5 |
| fallthrough.go:9:3:9:13 | expression statement | fallthrough.go:9:3:9:13 | Before call to test5 |
| fallthrough.go:9:9:9:12 | true | fallthrough.go:9:3:9:13 | call to test5 |
| fallthrough.go:11:2:11:6 | test5 | fallthrough.go:11:8:11:12 | false |
| fallthrough.go:11:2:11:13 | After call to test5 | fallthrough.go:11:2:11:13 | After expression statement |
| fallthrough.go:11:2:11:13 | After expression statement | fallthrough.go:3:36:12:1 | After block statement |
| fallthrough.go:11:2:11:13 | Before call to test5 | fallthrough.go:11:2:11:6 | test5 |
| fallthrough.go:11:2:11:13 | call to test5 | fallthrough.go:3:1:12:1 | Exceptional Exit |
| fallthrough.go:11:2:11:13 | call to test5 | fallthrough.go:11:2:11:13 | After call to test5 |
| fallthrough.go:11:2:11:13 | expression statement | fallthrough.go:11:2:11:13 | Before call to test5 |
| fallthrough.go:11:8:11:12 | false | fallthrough.go:11:2:11:13 | call to test5 |
| fallthrough.go:14:1:21:1 | Entry | fallthrough.go:14:32:14:32 | x |
| fallthrough.go:14:1:21:1 | Exceptional Exit | fallthrough.go:14:1:21:1 | Exit |
| fallthrough.go:14:1:21:1 | Normal Exit | fallthrough.go:14:1:21:1 | Exit |
| fallthrough.go:14:1:21:1 | function declaration | fallthrough.go:0:0:0:0 | After fallthrough.go |
| fallthrough.go:14:32:14:32 | x | fallthrough.go:14:39:21:1 | block statement |
| fallthrough.go:14:39:21:1 | After block statement | fallthrough.go:14:1:21:1 | Normal Exit |
| fallthrough.go:14:39:21:1 | block statement | fallthrough.go:15:2:19:2 | expression-switch statement |
| fallthrough.go:15:2:19:2 | After expression-switch statement | fallthrough.go:20:2:20:13 | expression statement |
| fallthrough.go:15:2:19:2 | expression-switch statement | fallthrough.go:15:9:15:9 | x |
| fallthrough.go:15:9:15:9 | x | fallthrough.go:16:2:17:13 | case clause |
| fallthrough.go:16:2:17:13 | After case clause [match] | fallthrough.go:17:3:17:13 | Before fallthrough statement |
| fallthrough.go:16:2:17:13 | After case clause [no-match] | fallthrough.go:18:2:18:9 | case clause |
| fallthrough.go:16:2:17:13 | case clause | fallthrough.go:16:7:16:7 | 0 |
| fallthrough.go:16:7:16:7 | 0 | fallthrough.go:16:7:16:7 | After 0 [match] |
| fallthrough.go:16:7:16:7 | 0 | fallthrough.go:16:7:16:7 | After 0 [no-match] |
| fallthrough.go:16:7:16:7 | After 0 [match] | fallthrough.go:16:2:17:13 | After case clause [match] |
| fallthrough.go:16:7:16:7 | After 0 [no-match] | fallthrough.go:16:2:17:13 | After case clause [no-match] |
| fallthrough.go:17:3:17:13 | Before fallthrough statement | fallthrough.go:17:3:17:13 | fallthrough statement |
| fallthrough.go:17:3:17:13 | fallthrough statement | fallthrough.go:15:2:19:2 | After expression-switch statement |
| fallthrough.go:18:2:18:9 | After case clause [match] | fallthrough.go:15:2:19:2 | After expression-switch statement |
| fallthrough.go:18:2:18:9 | case clause | fallthrough.go:18:2:18:9 | After case clause [match] |
| fallthrough.go:20:2:20:6 | test5 | fallthrough.go:20:8:20:12 | false |
| fallthrough.go:20:2:20:13 | After call to test5 | fallthrough.go:20:2:20:13 | After expression statement |
| fallthrough.go:20:2:20:13 | After expression statement | fallthrough.go:14:39:21:1 | After block statement |
| fallthrough.go:20:2:20:13 | Before call to test5 | fallthrough.go:20:2:20:6 | test5 |
| fallthrough.go:20:2:20:13 | call to test5 | fallthrough.go:14:1:21:1 | Exceptional Exit |
| fallthrough.go:20:2:20:13 | call to test5 | fallthrough.go:20:2:20:13 | After call to test5 |
| fallthrough.go:20:2:20:13 | expression statement | fallthrough.go:20:2:20:13 | Before call to test5 |
| fallthrough.go:20:8:20:12 | false | fallthrough.go:20:2:20:13 | call to test5 |
| generic.go:0:0:0:0 | After generic.go | generic.go:0:0:0:0 | Normal Exit |
| generic.go:0:0:0:0 | Entry | generic.go:0:0:0:0 | generic.go |
| generic.go:0:0:0:0 | Normal Exit | generic.go:0:0:0:0 | Exit |
Expand Down Expand Up @@ -2929,12 +3006,13 @@
| stmts.go:91:7:91:7 | After 1 [no-match] | stmts.go:91:2:93:13 | After case clause [no-match] |
| stmts.go:92:3:92:7 | test5 | stmts.go:92:9:92:13 | false |
| stmts.go:92:3:92:14 | After call to test5 | stmts.go:92:3:92:14 | After expression statement |
| stmts.go:92:3:92:14 | After expression statement | stmts.go:93:3:93:13 | fallthrough statement |
| stmts.go:92:3:92:14 | After expression statement | stmts.go:93:3:93:13 | Before fallthrough statement |
| stmts.go:92:3:92:14 | Before call to test5 | stmts.go:92:3:92:7 | test5 |
| stmts.go:92:3:92:14 | call to test5 | stmts.go:75:1:109:1 | Exceptional Exit |
| stmts.go:92:3:92:14 | call to test5 | stmts.go:92:3:92:14 | After call to test5 |
| stmts.go:92:3:92:14 | expression statement | stmts.go:92:3:92:14 | Before call to test5 |
| stmts.go:92:9:92:13 | false | stmts.go:92:3:92:14 | call to test5 |
| stmts.go:93:3:93:13 | Before fallthrough statement | stmts.go:93:3:93:13 | fallthrough statement |
| stmts.go:93:3:93:13 | fallthrough statement | stmts.go:95:3:95:13 | expression statement |
| stmts.go:94:2:95:13 | After case clause [match] | stmts.go:95:3:95:13 | expression statement |
| stmts.go:94:2:95:13 | After case clause [no-match] | stmts.go:90:2:96:2 | After expression-switch statement |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

func fallthroughToEmptyCase(x int) {
switch x {
case 0:
fallthrough
case 1:
default:
test5(true)
}
test5(false)
}

func fallthroughToEmptyDefault(x int) {
switch x {
case 0:
fallthrough
default:
}
test5(false)
}
4 changes: 4 additions & 0 deletions java/ql/lib/semmle/code/java/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ private module Ast implements AstSig<Location> {

class ContinueStmt = J::ContinueStmt;

class FallthroughStmt extends Stmt {
FallthroughStmt() { none() }
}

class GotoStmt extends Stmt {
GotoStmt() { none() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ module Ast implements AstSig<Py::Location> {
ContinueStmt() { this = TPyStmt(any(Py::Continue c)) }
}

/** A `fallthrough` statement. Python has no fallthrough. */
class FallthroughStmt extends Stmt {
FallthroughStmt() { none() }
}

/** A `goto` statement. Python has no goto. */
class GotoStmt extends Stmt {
GotoStmt() { none() }
Expand Down
4 changes: 4 additions & 0 deletions ruby/ql/lib/codeql/ruby/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ private module Ast implements AstSig<Location> {

class ContinueStmt extends Stmt instanceof R::Ast::NextStmt { }

class FallthroughStmt extends Stmt {
FallthroughStmt() { none() }
}

class LabeledStmt extends Stmt {
LabeledStmt() { none() }

Expand Down
26 changes: 26 additions & 0 deletions shared/controlflow/codeql/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ signature module AstSig<LocationSig Location> {
*/
class ContinueStmt extends Stmt;

/**
* A `fallthrough` statement.
*
* Fallthrough statements complete abruptly and continue at the next switch
* case's body, without evaluating its pattern or guard.
*/
class FallthroughStmt extends Stmt;

/**
* A `goto` statement.
*
Expand Down Expand Up @@ -579,6 +587,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
or
n instanceof ContinueStmt
or
n instanceof FallthroughStmt
or
n instanceof GotoStmt
or
n instanceof Expr and
Expand Down Expand Up @@ -743,6 +753,8 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
or
n instanceof ContinueStmt
or
n instanceof FallthroughStmt
or
n instanceof GotoStmt
or
n instanceof ReturnStmt
Expand Down Expand Up @@ -1288,6 +1300,9 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
ast instanceof ContinueStmt and
st instanceof ContinueSuccessor
or
ast instanceof FallthroughStmt and
st instanceof FallthroughSuccessor
or
ast instanceof GotoStmt and
st instanceof GotoSuccessor
|
Expand Down Expand Up @@ -1396,6 +1411,17 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
)
)
or
exists(Switch switch, int index, Case nextCase |
ast = getCaseBodyElement(switch.getCase(index), _) and
nextCase = switch.getCase(index + 1) and
c.asSimpleAbruptCompletion() instanceof FallthroughSuccessor
Comment thread
paldepind marked this conversation as resolved.
Resolved
|
n.isBefore(getFirstCaseBodyElement(nextCase))
or
not exists(getFirstCaseBodyElement(nextCase)) and
n.isAfter(switch)
)
or
exists(LabeledStmt target, Input1::Label l |
ast = target.getStmt() and
Input1::hasLabel(target, l) and
Expand Down
12 changes: 10 additions & 2 deletions shared/controlflow/codeql/controlflow/SuccessorType.qll
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* \- JumpSuccessor
* |- BreakSuccessor
* |- ContinueSuccessor
* |- FallthroughSuccessor
* |- GotoSuccessor
* |- RedoSuccessor // rare, used in Ruby
* \- RetrySuccessor // rare, used in Ruby
Expand Down Expand Up @@ -71,6 +72,7 @@ private newtype TSuccessorType =
TExitSuccessor() or
TBreakSuccessor() or
TContinueSuccessor() or
TFallthroughSuccessor() or
TGotoSuccessor() or
TRedoSuccessor() or
TRetrySuccessor()
Expand Down Expand Up @@ -357,13 +359,14 @@ class ExitSuccessor extends AbruptSuccessorImpl, TExitSuccessor {
}

private class TJumpSuccessor =
TBreakSuccessor or TContinueSuccessor or TGotoSuccessor or TRedoSuccessor or TRetrySuccessor;
TBreakSuccessor or TContinueSuccessor or TFallthroughSuccessor or TGotoSuccessor or
TRedoSuccessor or TRetrySuccessor;

/**
* A jump control flow successor.
*
* This covers non-exceptional, non-local control flow, such as `break`,
* `continue`, and `goto`.
* `continue`, `fallthrough`, and `goto`.
*/
abstract private class JumpSuccessorImpl extends AbruptSuccessorImpl, TJumpSuccessor { }

Expand All @@ -379,6 +382,11 @@ class ContinueSuccessor extends JumpSuccessorImpl, TContinueSuccessor {
override string toString() { result = "continue" }
}

/** A `fallthrough` control flow successor. */
class FallthroughSuccessor extends JumpSuccessorImpl, TFallthroughSuccessor {
override string toString() { result = "fallthrough" }
}

/** A `goto` control flow successor. */
class GotoSuccessor extends JumpSuccessorImpl, TGotoSuccessor {
override string toString() { result = "goto" }
Expand Down
4 changes: 4 additions & 0 deletions unified/extractor/ast_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ supertypes:
- do_while_stmt
- defer_stmt
- labeled_stmt
- fallthrough_stmt
callable:
subtypes:
- top_level
Expand Down Expand Up @@ -302,6 +303,9 @@ named:
continue_expr:
label_name_node?: identifier

# Transfer control directly to the next switch case's body.
fallthrough_stmt:

# A labeled statement, such as `outer: for ... { ... }`. The labeled
# statement appears as the `stmt` field; `break`/`continue` may target
# the label.
Expand Down
2 changes: 1 addition & 1 deletion unified/extractor/src/languages/swift/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
rule!((returnStmt expression: _? @val) => (return_expr value: {val})),
rule!((breakStmt label: _? @@lbl) => (break_expr label_name_node: [(identifier #{lbl})])),
rule!((continueStmt label: _? @@lbl) => (continue_expr label_name_node: [(identifier #{lbl})])),
rule!((fallThroughStmt) => (fallthrough_stmt)),
Comment thread
paldepind marked this conversation as resolved.
Resolved
rule!((throwStmt expression: @val) => (throw_expr value: {val})),
// ---- Closures ----
// A closure (`{ (x: Int) -> Int in … }`) becomes a `function_expr`. The
Expand Down Expand Up @@ -1635,7 +1636,6 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
rule!((classRestrictionType) => (unsupported_node "")),
rule!((compositionType) => (unsupported_node "")),
rule!((discardStmt) => (unsupported_node "")),
rule!((fallThroughStmt) => (unsupported_node "")),
rule!((implicitlyUnwrappedOptionalType) => (unsupported_node "")),
rule!((inlineArrayType) => (unsupported_node "")),
rule!((keyPathExpr) => (unsupported_node "")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ top_level source="⟨body⟩"
pattern: int_literal "1" source="1"
body:
block source="⟨stmt⟩"
stmt: unsupported_node "fallthrough" source="fallthrough"
stmt: fallthrough_stmt "fallthrough" source="fallthrough"
switch_case source="default:\n ⟨body⟩"
body:
block source="⟨stmt⟩"
Expand Down
8 changes: 8 additions & 0 deletions unified/ql/lib/codeql/unified/internal/Ast.qll
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ module Unified {
}
}

/** A class representing `fallthrough_stmt` tokens. */
class FallthroughStmt extends @unified_token_fallthrough_stmt, F::Stmt, F::Token {
/** Gets the name of the primary QL class for this element. */
final override string getAPrimaryQlClass() { result = "FallthroughStmt" }
}

/** A class representing `fixity` tokens. */
class Fixity extends @unified_token_fixity, F::AstNode, F::Token {
/** Gets the name of the primary QL class for this element. */
Expand Down Expand Up @@ -1998,6 +2004,8 @@ module UnifiedFinal {

final class ExprPattern = F::ExprPattern;

final class FallthroughStmt = F::FallthroughStmt;

final class Fixity = F::Fixity;

final class FloatLiteral = F::FloatLiteral;
Expand Down
2 changes: 2 additions & 0 deletions unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ module Ast implements AstSig<Location> {

class ContinueStmt = U::ContinueExpr;

class FallthroughStmt = U::FallthroughStmt;

class LabeledStmt = U::LabeledStmt;

class GotoStmt extends Stmt {
Expand Down
Loading
Loading