Skip to content

Commit fba894a

Browse files
committed
Fix goto targeting stacked label
1 parent 7631a01 commit fba894a

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

‎go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -836,13 +836,23 @@ module CfgImpl {
836836
n.isAdditional(fd.getBody(), "result-read:0")
837837
)
838838
or
839-
// Function bodies are excluded from `Ast::BlockStmt`, so handle goto
840-
// targets among their top-level statements here.
841-
exists(Go::FuncDef fd, Go::Stmt target, Label l |
842-
ast = fd.getBody() and
843-
target = fd.getBody().getAStmt() and
839+
// Handle goto targets that the shared block logic cannot see: top-level
840+
// statements of function bodies and labels enclosing the current node.
841+
exists(Go::Stmt target, Label l |
842+
(
843+
exists(Go::FuncDef fd |
844+
ast = fd.getBody() and
845+
target = fd.getBody().getAStmt() and
846+
hasLabel(target, l)
847+
)
848+
or
849+
exists(Go::LabeledStmt lbl |
850+
ast = lbl.getStmt() and
851+
target = lbl and
852+
l = lbl.getLabel()
853+
)
854+
) and
844855
not target instanceof Go::GotoStmt and
845-
hasLabel(target, l) and
846856
n.isBefore(target) and
847857
c.getSuccessorType() instanceof GotoSuccessor and
848858
c.hasLabel(l)

‎go/ql/test/library-tests/semmle/go/controlflow/GotoTarget/gotos.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ inner:
1010
goto done // $ gotoTarget=done
1111

1212
back:
13-
goto inner // $ MISSING: gotoTarget=inner
13+
goto inner // $ gotoTarget=inner
1414
done:
1515
flag = false
1616
}

0 commit comments

Comments
 (0)