Skip to content

Shared CFG: fix nested and self-looping goto targets - #22687

Open
owen-mc wants to merge 2 commits into
github:mainfrom
owen-mc:shared/cfg/goto-targets
Open

owen-mc wants to merge 2 commits into
github:mainfrom
owen-mc:shared/cfg/goto-targets

Conversation

@owen-mc

@owen-mc owen-mc commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Fix nested and self-looping goto targets in the shared CFG by adding labeled statements to the shared AST interface and handling label propagation centrally.

Labels now propagate inward for labeled break/continue and outward through nested labeled statements for goto, avoiding incorrect extra targets. The change updates Go, Java, and C# integrations and adds Go tests for stacked labels, sibling labels, and direct self-loops.

@owen-mc
owen-mc requested review from a team as code owners September 28, 2026 09:37
Copilot AI balanced review requested due to automatic review settings September 28, 2026 09:37
@owen-mc owen-mc added the no-change-note-required This PR does not need a change note label Sep 28, 2026
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed

Copilot AI left a comment

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.

Copilot review overview

🟡 Changes recommended

External Go gotos targeting an inner stacked label still fail to connect to their destination.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Generalizes shared CFG abrupt completions to carry resolved jump targets, enabling precise Go and C# goto handling.

Changes:

  • Adds resolved JumpTarget support to the shared CFG.
  • Implements target resolution for Go and C#.
  • Adds Go regression coverage and updates other language integrations.
File Description
shared/​controlflow/​codeql/​controlflow/​ControlFlowGraph.qll Adds targeted abrupt-completion propagation.
go/​ql/​lib/​semmle/​go/​controlflow/​ControlFlowGraphImpl.qll Resolves Go goto destinations.
go/​ql/​test/​library-tests/​semmle/​go/​controlflow/​GotoTarget/​GotoTarget.ql Queries goto target reachability.
go/​ql/​test/​library-tests/​semmle/​go/​controlflow/​GotoTarget/​GotoTarget.expected Records expected test results.
go/​ql/​test/​library-tests/​semmle/​go/​controlflow/​GotoTarget/​gotos.go Supplies stacked-label test cases.
csharp/​ql/​lib/​semmle/​code/​csharp/​controlflow/​ControlFlowGraph.qll Resolves label, case, and default gotos.
java/​ql/​lib/​semmle/​code/​java/​ControlFlowGraph.qll Implements the extended shared interface.
python/​ql/​lib/​semmle/​python/​controlflow/​internal/​AstNodeImpl.qll Implements the extended shared interface.
ruby/​ql/​lib/​codeql/​ruby/​controlflow/​ControlFlowGraph.qll Implements the extended shared interface.
unified/​ql/​lib/​codeql/​unified/​internal/​ControlFlowGraph.qll Implements the extended shared interface.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Outdated
@owen-mc
owen-mc force-pushed the shared/cfg/goto-targets branch 2 times, most recently from ba548eb to 86ead25 Compare September 28, 2026 23:43
@owen-mc
owen-mc requested a review from aschackmull September 29, 2026 08:28
@owen-mc
owen-mc force-pushed the shared/cfg/goto-targets branch from 86ead25 to 832228f Compare September 29, 2026 08:42
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed
@owen-mc
owen-mc force-pushed the shared/cfg/goto-targets branch from 832228f to 22dea67 Compare September 30, 2026 15:27
@owen-mc owen-mc changed the title Shared CFG: generalize targeted abrupt completions Shared CFG: fix nested labeled goto targets Sep 30, 2026
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Fixed
@owen-mc owen-mc changed the title Shared CFG: fix nested labeled goto targets Shared CFG: fix nested and self-looping goto targets Sep 30, 2026
@owen-mc
owen-mc force-pushed the shared/cfg/goto-targets branch from 8e98770 to e7d3ae3 Compare September 30, 2026 15:56
@owen-mc
owen-mc force-pushed the shared/cfg/goto-targets branch from c08ea98 to fc044f4 Compare September 30, 2026 22:04
@owen-mc

owen-mc commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

@aschackmull I've updated this PR to implement the approach you outlined in your review of Go: fix CFG for nested labels. Please can you have a look.

Copilot AI left a comment

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.

Copilot review overview

🔵 Needs a closer look

The shared CFG change affects several language adapters, warranting final human cross-language validation.

Review effort: Balanced
Findings: None

Resolved since last review (1)

Comment thread go/ql/test/library-tests/semmle/go/controlflow/GotoTarget/gotos.go
Comment thread shared/controlflow/codeql/controlflow/ControlFlowGraph.qll Outdated
Comment on lines +1288 to +1293
/** Holds if `n` has `l`, possibly through enclosing labeled statements. */
private predicate hasLabel(AstNode n, Input1::Label l) {
Input1::hasLabel(n, l)
or
exists(LabeledStmt labeled | labeled.getStmt() = n and hasLabel(labeled, l))
}

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 predicate is semantically different from Input1::hasLabel, so it should have a different name. Also, it's never needed for nodes, n, for which Input1::hasLabel(n, _) holds so we may use strict enclosure. That means we can simplify using a TC:

Suggested change
/** Holds if `n` has `l`, possibly through enclosing labeled statements. */
private predicate hasLabel(AstNode n, Input1::Label l) {
Input1::hasLabel(n, l)
or
exists(LabeledStmt labeled | labeled.getStmt() = n and hasLabel(labeled, l))
}
/** Holds if `n` is marked with a `LabeledStmt` with label `l`. */
private predicate hasEnclosingLabel(AstNode n, Input1::Label l) {
exists(LabeledStmt labeled | labeled.getStmt+() = n and Input1::hasLabel(labeled, l))
}

Comment thread shared/controlflow/codeql/controlflow/ControlFlowGraph.qll Outdated
Comment thread go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll Outdated
Comment thread shared/controlflow/codeql/controlflow/ControlFlowGraph.qll Outdated
Comment thread shared/controlflow/codeql/controlflow/ControlFlowGraph.qll Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants