Skip to content

Commit

Permalink
fix: parser hangs when a bracket isn't closed (#1005) (#1029)
Browse files Browse the repository at this point in the history
Co-authored-by: harrisbisset <hb233@st-andrews.ac.uk>
Co-authored-by: Harris Bisset <harrisbisset@gmail.com>
Co-authored-by: Adrian Hesketh <adrianhesketh@hushmail.com>
  • Loading branch information
4 people authored Dec 29, 2024
1 parent 9a97564 commit 9058914
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions parser/v2/goexpression/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ var expressionTests = []testInput{
name: "string concat",
input: `direction + "newest"`,
},
{
name: "function call",
input: `SplitRule(types.GroupMember{
UserID: uuid.NewString(),
Username: "user me",
}, []types.GroupMember{
{
UserID: uuid.NewString(),
Username: "user 1",
},
})`,
},
}

func TestExpression(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions parser/v2/goexpression/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func (ep *ExpressionParser) Insert(
defer func() {
ep.Previous = tok
}()

// If we've reach the end of the file, terminate reading.
if tok == token.EOF {
// If the EOF was reached, but we're not at the top level, we must have an unbalanced expression.
if !ep.isTopLevel() {
return true, ErrUnbalanced{ep.Stack.Pop()}
}
return true, nil
}

// Handle function literals e.g. func() { fmt.Println("Hello") }
// By pushing the current depth onto the stack, we prevent stopping
// until we've closed the function.
Expand Down
33 changes: 33 additions & 0 deletions parser/v2/templelementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,36 @@ func TestTemplElementExpressionParser(t *testing.T) {
})
}
}

func TestTemplElementExpressionParserFailures(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "templelement: missing closing brace",
input: `@SplitRule(types.GroupMember{
UserID: uuid.NewString(),
Username: "user me",
}, []types.GroupMember{
{
UserID: uuid.NewString(),
Username: "user 1",
},
`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
input := parse.NewInput(tt.input)
_, ok, err := templElementExpression.Parse(input)
if err == nil {
t.Fatalf("expected an error")
}
if ok {
t.Fatalf("expected a failure")
}
})
}
}

0 comments on commit 9058914

Please sign in to comment.