Skip to content

Commit

Permalink
fix: parse failure when template files are > 8KB in size
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jan 30, 2022
1 parent 0a04450 commit 17fe039
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/PuerkitoBio/goquery v1.7.1
github.com/a-h/lexical v0.0.47
github.com/a-h/lexical v0.0.50
github.com/a-h/pathvars v0.0.0-20200320143331-78b263b728e2
github.com/google/go-cmp v0.5.6
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -16,7 +16,7 @@ require (
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.17.0
golang.org/x/mod v0.5.1 // indirect
golang.org/x/mod v0.5.1
)

//replace github.com/a-h/lexical => /Users/adrian/github.com/a-h/lexical
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4=
github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY=
github.com/a-h/lexical v0.0.47 h1:8VshFdJrb1wnDiA1bFqoxPhXsxpMqPFAsLcwLNQ8rq4=
github.com/a-h/lexical v0.0.47/go.mod h1:d73jw5cgKXuYypRozNBuxRNFrTWQ3y5hVMG7rUjh1Qw=
github.com/a-h/lexical v0.0.48 h1:vliyXGKMmEUP2hg8m+zq2vyE04iYNWvq1V3hH2QSdcE=
github.com/a-h/lexical v0.0.48/go.mod h1:d73jw5cgKXuYypRozNBuxRNFrTWQ3y5hVMG7rUjh1Qw=
github.com/a-h/lexical v0.0.49 h1:TRnSxSI7M7zAz/R5DFjL9kbX3tQgQ6BsvXe3VwpMtw0=
github.com/a-h/lexical v0.0.49/go.mod h1:d73jw5cgKXuYypRozNBuxRNFrTWQ3y5hVMG7rUjh1Qw=
github.com/a-h/lexical v0.0.50 h1:QGyK1vn51J3RyWgoMGdoObcCHcyqtEVBaGmQHCzhLog=
github.com/a-h/lexical v0.0.50/go.mod h1:d73jw5cgKXuYypRozNBuxRNFrTWQ3y5hVMG7rUjh1Qw=
github.com/a-h/pathvars v0.0.0-20200320143331-78b263b728e2 h1:OObPWjEG3FZsjTNQPATyh8IpAvUfB6WpIO4mSzRSbRc=
github.com/a-h/pathvars v0.0.0-20200320143331-78b263b728e2/go.mod h1:QulNSBbUWcOdt7MTypwParV0/gDtr8qRBmQAiDdOnS4=
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
Expand Down
4 changes: 2 additions & 2 deletions parser/doctypeparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func TestDocTypeParserErrors(t *testing.T) {
Col: 0,
},
Position{
Index: 17,
Index: 15,
Line: 1,
Col: 17,
Col: 14,
}),
},
{
Expand Down
17 changes: 17 additions & 0 deletions parser/elementparser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"strings"
"testing"

"github.com/a-h/lexical/input"
Expand Down Expand Up @@ -575,3 +576,19 @@ func TestElementParserErrors(t *testing.T) {
})
}
}

func TestBigElement(t *testing.T) {
var sb strings.Builder
sb.WriteString("<div>")
for i := 0; i < 4096*4; i++ {
sb.WriteString("a")
}
sb.WriteString("</div>")
result := newElementParser().Parse(input.NewFromString(sb.String()))
if result.Error != nil {
t.Fatalf("unexpected error: %v", result.Error)
}
if !result.Success {
t.Errorf("unexpected failure to parse")
}
}
4 changes: 2 additions & 2 deletions parser/packageparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func TestPackageParserErrors(t *testing.T) {
Col: 11,
},
Position{
Index: 15,
Index: 12,
Line: 1,
Col: 15,
Col: 11,
},
),
},
Expand Down
12 changes: 11 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,23 @@ func (p TemplateFileParser) Parse(pi parse.Input) parse.Result {
return parse.Success("template file", tf, nil)
}

const maxBufferSize = 1024 * 1024 * 10 // 10MB

func Parse(fileName string) (TemplateFile, error) {
f, err := os.Open(fileName)
if err != nil {
return TemplateFile{}, err
}
fi, err := f.Stat()
if err != nil {
return TemplateFile{}, err
}
bufferSize := maxBufferSize
if fi.Size() < int64(bufferSize) {
bufferSize = int(fi.Size())
}
reader := bufio.NewReader(f)
tfr := NewTemplateFileParser().Parse(input.New(reader))
tfr := NewTemplateFileParser().Parse(input.NewWithBufferSize(reader, bufferSize))
if tfr.Error != nil {
return TemplateFile{}, tfr.Error
}
Expand Down

0 comments on commit 17fe039

Please sign in to comment.