Skip to content

Commit

Permalink
parser: don't hang if heredoc errors
Browse files Browse the repository at this point in the history
We would tokenize whatever bytes were left, which meant that we could be
overwriting tok = EOF with garbage. In this case, with RPAREN for ).

In some weird cases like this one, the parser would get confused and
loop forever.
  • Loading branch information
mvdan committed Oct 13, 2016
1 parent 5c2b6f3 commit 82a3330
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ var shellTests = []struct {
"<<EOF\n$(()a",
`2:1: reached EOF without matching $(( with ))`,
},
{
"<<EOF\n`))",
`2:2: ) can only be used to close a subshell`,
},
{
"echo ${foo",
`1:6: reached EOF without matching ${ with }`,
Expand Down
3 changes: 3 additions & 0 deletions parser/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ skipSpace:
p.newLine = true
if len(p.heredocs) > p.buriedHdocs {
p.doHeredocs()
if p.tok == token.EOF {
return
}
}
case '\\':
if p.npos < len(p.src)-1 && p.src[p.npos+1] == '\n' {
Expand Down

0 comments on commit 82a3330

Please sign in to comment.