Skip to content

Commit

Permalink
jsonio: Fix parse errors on EOF
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 committed Feb 29, 2024
1 parent 87deeeb commit 1d3d2c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/jsonlexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func (l *Lexer) readString() Token {
c, err := l.br.ReadByte()
if err != nil {
l.err = err
if err == io.EOF {
l.err = errors.New("unexpected end of JSON input")
}
return TokenErr
}
l.buf = append(l.buf, c)
Expand Down
7 changes: 6 additions & 1 deletion 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 @@ -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 {
return errors.New("unexpected end of JSON input")
}
return err
}
return fmt.Errorf("invalid character %q %s", r.lexer.Buf()[0], msg)
}
28 changes: 28 additions & 0 deletions zio/jsonio/ztests/unexpected-input-end.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
script: |
while IFS= read -r line; do
! echo "$line" | zq -i json -
done < errors.txt
inputs:
- name: errors.txt
data: |
"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
- name: stdout
data: ""

0 comments on commit 1d3d2c3

Please sign in to comment.