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
34 changes: 34 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,14 @@ ERROR: <input>:1:34: Syntax error: expected ']'
| [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
| .................................^`,
},
{
I: "a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : " +
"a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : " +
"a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : " +
"a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : " +
"a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : a ? b : c",
E: "ERROR: <input>:-1:0: expression recursion limit exceeded: 32",
},
{
I: `-[-1--1--1--1---1--1--1--0--1--1--1--1--0--2--1--1--0--1--1--1--1--0--1--1--1
--3-[-1--1--1--1---1--1--1--0--1--1--1--1--0--3--1--1--0--1--1--1--1--0--1--1--1
Expand Down Expand Up @@ -2854,9 +2862,35 @@ func TestRecursionLimit(t *testing.T) {
t.Errorf("expected recursion limit error, got none")
}
})

t.Run("DeeplyNestedTernaryLimitExceeded", func(t *testing.T) {
p, err := NewParser(MaxRecursionDepth(4), EnablePrattParser(pratt))
if err != nil {
t.Fatalf("NewParser() failed: %v", err)
}
_, errs := p.Parse(common.NewTextSource("a ? b : a ? b : a ? b : a ? b : a ? b : c"))
if len(errs.GetErrors()) == 0 {
t.Errorf("expected recursion limit error, got none")
}
})
})
}

t.Run("PrattDeeplyNestedTernary", func(t *testing.T) {
p, err := NewParser(MaxRecursionDepth(4), EnablePrattParser(true))
if err != nil {
t.Fatalf("NewParser() failed: %v", err)
}
_, errs := p.Parse(common.NewTextSource("a ? b : a ? b : a ? b : a ? b : c"))
if len(errs.GetErrors()) > 0 {
t.Errorf("unexpected recursion limit error at depth 4: %s", errs.ToDisplayString())
}
_, errs = p.Parse(common.NewTextSource("a ? b : a ? b : a ? b : a ? b : a ? b : c"))
if len(errs.GetErrors()) == 0 {
t.Errorf("expected recursion limit error at depth 5, got none")
}
})

t.Run("PrattSequentialScopesDoNotAccumulateDepth", func(t *testing.T) {
p, err := NewParser(MaxRecursionDepth(2), EnablePrattParser(true))
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion parser/pratt_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func (p *prattParserWorker) parseExpr() ast.Expr {

func (p *prattParserWorker) parseBinaryAndTernary(minPrec int) ast.Expr {
lhs := p.parseSelectorChain()
for {
for !p.recursionLimitExceeded && !p.isRecoveryLimitExceeded() {
tok := p.peekTok.kind
if tok == tokQuestion && minPrec <= 0 {
lhs = p.parseTernary(lhs)
Expand All @@ -438,13 +438,21 @@ func (p *prattParserWorker) parseBinaryAndTernary(minPrec int) ast.Expr {
}

func (p *prattParserWorker) parseTernary(lhs ast.Expr) ast.Expr {
if p.recursionDepth > p.maxRecursionDepth {
p.recursionLimitExceeded = true
p.errors.internalError(fmt.Sprintf("expression recursion limit exceeded: %d", p.maxRecursionDepth))
return lhs
}
p.recursionDepth++
qTok := p.nextToken()
opID := p.nextID(qTok)
trueExpr := p.parseBinaryAndTernary(1)
if !p.expect(tokColon, "expected ':' in conditional expression") {
p.recursionDepth--
return lhs
}
falseExpr := p.parseBinaryAndTernary(0)
p.recursionDepth--
return p.helper.newGlobalCall(opID, operators.Conditional, lhs, trueExpr, falseExpr)
}

Expand Down
Loading