From 4a89913a9b9fa9e1ae4f9117c8e7099817f56a06 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 3 Apr 2020 10:33:05 +1100 Subject: [PATCH] Ban duplicate loop labels --- lang/ast/ast.go | 11 ++++++++++- lang/parse/parse.go | 10 ++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index ef3184b6..363c9a7d 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -257,8 +257,17 @@ type Loop interface { type LoopStack []Loop -func (s *LoopStack) Push(l Loop) { +// Push returns false if an existing Loop has the same non-zero label. +func (s *LoopStack) Push(l Loop) (ok bool) { + if label := l.Label(); label != 0 { + for _, o := range *s { + if label == o.Label() { + return false + } + } + } *s = append(*s, l) + return true } func (s *LoopStack) Pop() Loop { diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 8b98dc6f..ad1a2276 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -797,7 +797,10 @@ func (p *parser) parseStatement1() (*a.Node, error) { } n := a.NewWhile(label, condition, asserts) - p.loops.Push(n) + if !p.loops.Push(n) { + return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`, + label.Str(p.tm), p.filename, p.line()) + } body, err := p.parseBlock(doubleCurly) if err != nil { return nil, err @@ -1114,7 +1117,10 @@ func (p *parser) parseIterateBlock(label t.ID, assigns []*a.Node) (*a.Iterate, e } n := a.NewIterate(label, assigns, length, unroll, asserts) // TODO: decide how break/continue work with iterate loops. - p.loops.Push(n) + if !p.loops.Push(n) { + return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`, + label.Str(p.tm), p.filename, p.line()) + } body, err := p.parseBlock(false) if err != nil { return nil, err