From 17fe039105dfee957aa781a282d53d229e0b0ec5 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sun, 30 Jan 2022 18:41:28 +0000 Subject: [PATCH] fix: parse failure when template files are > 8KB in size --- go.mod | 4 ++-- go.sum | 8 ++++++-- parser/doctypeparser_test.go | 4 ++-- parser/elementparser_test.go | 17 +++++++++++++++++ parser/packageparser_test.go | 4 ++-- parser/parser.go | 12 +++++++++++- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 0c432f7b4..48e922a62 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index d886b74bb..e18c9f81d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/parser/doctypeparser_test.go b/parser/doctypeparser_test.go index 84570edcf..bb12e6e41 100644 --- a/parser/doctypeparser_test.go +++ b/parser/doctypeparser_test.go @@ -76,9 +76,9 @@ func TestDocTypeParserErrors(t *testing.T) { Col: 0, }, Position{ - Index: 17, + Index: 15, Line: 1, - Col: 17, + Col: 14, }), }, { diff --git a/parser/elementparser_test.go b/parser/elementparser_test.go index ca9bb1538..4e8646b68 100644 --- a/parser/elementparser_test.go +++ b/parser/elementparser_test.go @@ -1,6 +1,7 @@ package parser import ( + "strings" "testing" "github.com/a-h/lexical/input" @@ -575,3 +576,19 @@ func TestElementParserErrors(t *testing.T) { }) } } + +func TestBigElement(t *testing.T) { + var sb strings.Builder + sb.WriteString("
") + for i := 0; i < 4096*4; i++ { + sb.WriteString("a") + } + sb.WriteString("
") + 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") + } +} diff --git a/parser/packageparser_test.go b/parser/packageparser_test.go index 8e8b1dc97..c1b5936b9 100644 --- a/parser/packageparser_test.go +++ b/parser/packageparser_test.go @@ -25,9 +25,9 @@ func TestPackageParserErrors(t *testing.T) { Col: 11, }, Position{ - Index: 15, + Index: 12, Line: 1, - Col: 15, + Col: 11, }, ), }, diff --git a/parser/parser.go b/parser/parser.go index 313c0873d..3325697c6 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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 }