Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonio: Fix parse errors on EOF #5055

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/jsonlexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions zio/jsonio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonio

import (
"bufio"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
34 changes: 34 additions & 0 deletions zio/jsonio/ztests/unexpected-input-end.yaml
Original file line number Diff line number Diff line change
@@ -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: ""
Loading