diff --git a/pkg/jsonlexer/lexer.go b/pkg/jsonlexer/lexer.go index 9b2ab7da34..72e361a0c3 100644 --- a/pkg/jsonlexer/lexer.go +++ b/pkg/jsonlexer/lexer.go @@ -93,6 +93,9 @@ func (l *Lexer) readLiteral(s string, t Token) Token { for i := range s { c, err := l.br.ReadByte() if err != nil { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } l.err = err return TokenErr } @@ -160,6 +163,9 @@ func (l *Lexer) readString() Token { c, err := l.br.ReadByte() if err != nil { l.err = err + if err == io.EOF { + l.err = io.ErrUnexpectedEOF + } return TokenErr } l.buf = append(l.buf, c) diff --git a/zio/jsonio/reader.go b/zio/jsonio/reader.go index d44da87c06..de357e0554 100644 --- a/zio/jsonio/reader.go +++ b/zio/jsonio/reader.go @@ -2,6 +2,7 @@ package jsonio import ( "bufio" + "errors" "fmt" "io" @@ -35,7 +36,7 @@ func (r *Reader) Read() (*zed.Value, error) { if err == io.EOF { return nil, nil } - return nil, err + return nil, r.error(t, "") } r.builder.reset() if err := r.handleToken("", t); err != nil { @@ -149,7 +150,11 @@ func (r *Reader) readNameValuePair(t jsonlexer.Token) error { func (r *Reader) error(t jsonlexer.Token, msg string) error { if t == jsonlexer.TokenErr { - return r.lexer.Err() + err := r.lexer.Err() + if err == io.EOF || err == io.ErrUnexpectedEOF { + return errors.New("unexpected end of JSON input") + } + return err } return fmt.Errorf("invalid character %q %s", r.lexer.Buf()[0], msg) } diff --git a/zio/jsonio/ztests/unexpected-input-end.yaml b/zio/jsonio/ztests/unexpected-input-end.yaml new file mode 100644 index 0000000000..24415c3221 --- /dev/null +++ b/zio/jsonio/ztests/unexpected-input-end.yaml @@ -0,0 +1,34 @@ +script: | + while IFS= read -r line; do + ! printf "$line" | zq -i json - + done < errors.txt + +inputs: + - name: errors.txt + data: | + tru + fal + nu + "3 + ["3", + ["3" + {"foo": + {"foo":"bar", + {"foo":"bar" + {"foo":"bar + +outputs: + - name: stderr + data: | + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + stdio:stdin: unexpected end of JSON input + - name: stdout + data: ""