Skip to content

Commit 7103ea0

Browse files
francislavoiemholt
andauthored
caddyfile: Fix case where heredoc marker is empty after newline (#5769)
Fixes `panic: runtime error: slice bounds out of range [:3] with capacity 2` Co-authored-by: Matt Holt <[email protected]>
1 parent 888c6d7 commit 7103ea0

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

caddyconfig/caddyfile/lexer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func (l *lexer) next() (bool, error) {
154154
// we reset the val because the heredoc is syntax we don't
155155
// want to keep.
156156
if ch == '\n' {
157+
if len(val) == 2 {
158+
return false, fmt.Errorf("missing opening heredoc marker on line #%d; must contain only alpha-numeric characters, dashes and underscores; got empty string", l.line)
159+
}
160+
157161
// check if there's too many <
158162
if string(val[:3]) == "<<<" {
159163
return false, fmt.Errorf("too many '<' for heredoc on line #%d; only use two, for example <<END", l.line)

caddyconfig/caddyfile/lexer_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ EOF same-line-arg
409409
},
410410
},
411411
},
412+
{
413+
input: []byte("not-a-heredoc <<\n"),
414+
expectErr: true,
415+
errorMessage: "missing opening heredoc marker on line #1; must contain only alpha-numeric characters, dashes and underscores; got empty string",
416+
},
412417
{
413418
input: []byte(`heredoc <<<EOF
414419
content

0 commit comments

Comments
 (0)