Fix dockerfile parser failing silently on long tokens

Signed-off-by: Daniel Nephin <dnephin@docker.com>

rewritten from github.com/moby/moby 59ad3a36e2684bd36a4b02179949bd17f1406918
docker-18.09
Daniel Nephin 2017-11-07 18:27:49 -05:00 committed by Tonis Tiigi
parent 182b2d5bd6
commit 2dc37b2ae6
2 changed files with 23 additions and 1 deletions

View File

@ -321,7 +321,7 @@ func Parse(rwc io.Reader) (*Result, error) {
Warnings: warnings, Warnings: warnings,
EscapeToken: d.escapeToken, EscapeToken: d.escapeToken,
OS: d.platformToken, OS: d.platformToken,
}, nil }, handleScannerError(scanner.Err())
} }
func trimComments(src []byte) []byte { func trimComments(src []byte) []byte {
@ -358,3 +358,12 @@ func processLine(d *Directive, token []byte, stripLeftWhitespace bool) ([]byte,
} }
return trimComments(token), d.possibleParserDirective(string(token)) return trimComments(token), d.possibleParserDirective(string(token))
} }
func handleScannerError(err error) error {
switch err {
case bufio.ErrTooLong:
return errors.Errorf("dockerfile line greater than max allowed size of %d", bufio.MaxScanTokenSize-1)
default:
return err
}
}

View File

@ -1,12 +1,14 @@
package parser package parser
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -159,3 +161,14 @@ RUN indented \
assert.Contains(t, warnings[1], "RUN another thing") assert.Contains(t, warnings[1], "RUN another thing")
assert.Contains(t, warnings[2], "will become errors in a future release") assert.Contains(t, warnings[2], "will become errors in a future release")
} }
func TestParseReturnsScannerErrors(t *testing.T) {
label := strings.Repeat("a", bufio.MaxScanTokenSize)
dockerfile := strings.NewReader(fmt.Sprintf(`
FROM image
LABEL test=%s
`, label))
_, err := Parse(dockerfile)
assert.EqualError(t, err, "dockerfile line greater than max allowed size of 65535")
}