Skip to content
Open
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
61 changes: 58 additions & 3 deletions shared/controlflow/codeql/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ signature module AstSig<LocationSig Location> {
*/
default AstNode getTryElse(TryStmt try) { none() }

/**
* Gets the `else` block of this `while` loop statement, if any.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Gets the `else` block of this `while` loop statement, if any.
* Gets the `else` block of `while` loop statement `loop`, if any.

*
* Only some languages (e.g. Python) support `while-else` constructs.
*/
default AstNode getWhileElse(WhileStmt loop) { none() }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to just one new predicate

Suggested change
default AstNode getWhileElse(WhileStmt loop) { none() }
default AstNode getLoopElse(LoopStmt loop) { none() }

The semantics of a loop-else block is mainly related to whether the loop exits via its condition or a break, so it's not really tied to the specifics of while vs. foreach.


/**
* Gets the `else` block of this `foreach` loop statement, if any.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Gets the `else` block of this `foreach` loop statement, if any.
* Gets the `else` block of `foreach` loop statement `loop`, if any.

*
* Only some languages (e.g. Python) support `for-else` constructs.
*/
default AstNode getForeachElse(ForeachStmt loop) { none() }

/**
* Gets the type expression of this catch clause, if any.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Gets the type expression of this catch clause, if any.
* Gets the type expression of catch clause `catch`, if any.

*
* In Python, the catch type is a runtime-evaluated expression
* (e.g. `except SomeException:` where `SomeException` is an
* arbitrary expression). For languages where the catch type is
* statically resolved, this defaults to `none()` and no CFG node
* is created.
*/
default Expr getCatchType(CatchClause catch) { none() }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the PR description - was this meant to go in a different PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, unless absolutely necessary, I don't think this should be a point of parameterisation - rather, I think we ought to be able to come up with a CFG for catch that solves the needs of all languages. We should probably discuss this offline and put a fix in a different PR when we agree on the solution.


/** A catch clause in a try statement. */
class CatchClause extends AstNode {
/** Gets the variable declared by this catch clause. */
Expand Down Expand Up @@ -1549,19 +1574,32 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
n2.isBefore(loopstmt.getBody())
or
n1.isAfterFalse(cond) and
n2.isAfter(loopstmt)
(
n2.isBefore(getWhileElse(loopstmt))
or
not exists(getWhileElse(loopstmt)) and n2.isAfter(loopstmt)
)
or
n1.isAfter(loopstmt.getBody()) and
n2.isAdditional(loopstmt, loopHeaderTag())
)
or
exists(WhileStmt whilestmt |
n1.isAfter(getWhileElse(whilestmt)) and
n2.isAfter(whilestmt)
)
or
exists(ForeachStmt foreachstmt |
n1.isBefore(foreachstmt) and
n2.isBefore(foreachstmt.getCollection())
or
n1.isAfterValue(foreachstmt.getCollection(),
any(EmptinessSuccessor t | t.getValue() = true)) and
n2.isAfter(foreachstmt)
(
n2.isBefore(getForeachElse(foreachstmt))
or
not exists(getForeachElse(foreachstmt)) and n2.isAfter(foreachstmt)
)
or
n1.isAfterValue(foreachstmt.getCollection(),
any(EmptinessSuccessor t | t.getValue() = false)) and
Expand All @@ -1574,10 +1612,17 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
n2.isAdditional(foreachstmt, loopHeaderTag())
or
n1.isAdditional(foreachstmt, loopHeaderTag()) and
n2.isAfter(foreachstmt)
(
n2.isBefore(getForeachElse(foreachstmt))
or
not exists(getForeachElse(foreachstmt)) and n2.isAfter(foreachstmt)
)
or
n1.isAdditional(foreachstmt, loopHeaderTag()) and
n2.isBefore(foreachstmt.getVariable())
or
n1.isAfter(getForeachElse(foreachstmt)) and
n2.isAfter(foreachstmt)
)
or
exists(ForStmt forstmt, PreControlFlowNode condentry |
Expand Down Expand Up @@ -1671,6 +1716,16 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
exists(CatchClause catchclause |
exists(MatchingSuccessor t |
n1.isBefore(catchclause) and
(
n2.isBefore(getCatchType(catchclause))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t is unbound in this disjunct.

or
not exists(getCatchType(catchclause)) and n2.isAfterValue(catchclause, t)
) and
if Input1::catchAll(catchclause) then t.getValue() = true else any()
)
or
exists(MatchingSuccessor t |
n1.isAfter(getCatchType(catchclause)) and
n2.isAfterValue(catchclause, t) and
if Input1::catchAll(catchclause) then t.getValue() = true else any()
)
Expand Down
Loading