Skip to content

Commit

Permalink
jsonio: Fix parse errors on EOF (#5055)
Browse files Browse the repository at this point in the history
Fix an issue with jsonio.Reader where parse errors occurring at the end
of an input (EOF) are often ignored or return an ambiguous EOF error.
  • Loading branch information
mattnibs authored Feb 29, 2024
1 parent e212199 commit 36b0d26
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
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: ""

0 comments on commit 36b0d26

Please sign in to comment.