Skip to content
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
8 changes: 7 additions & 1 deletion sio/jsonio/valreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (r *valReader) Next() ([]byte, error) {
return nil, io.EOF
}
start, end := jsonskip.Skip(r.cursor)
if start < 0 {
// A number ending at the end of the buffer may continue in input
// not yet read, so read more first.
if start < 0 || (end == len(r.cursor) && !r.EOF && isNumberStart(r.cursor[start])) {
if !hasFilled && !r.EOF {
if err := r.fill(); err != nil {
return nil, err
Expand Down Expand Up @@ -101,6 +103,10 @@ func firstNonWhitespaceCharacter(b []byte) int {
})
}

func isNumberStart(c byte) bool {
return c == '-' || (c >= '0' && c <= '9')
}

func (r *valReader) parseError() error {
return fmt.Errorf("line %d: invalid JSON value", r.lineNumber())
}
56 changes: 56 additions & 0 deletions sio/jsonio/valreader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package jsonio

import (
"io"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

// A number that straddles or ends at the end of the read buffer is read
// whole, whether more input or EOF follows.
func TestValReaderNumberAtBufferEnd(t *testing.T) {
size := len(NewValReader(nil).buf)
for _, num := range []string{"12345", "-42", "12.5", "1.5e10"} {
for k := 1; k <= len(num); k++ {
// A string fills the buffer up to the number's first k bytes.
pad := `"` + strings.Repeat("x", size-k-3) + "\"\n"
for _, rest := range []string{"\n2\n", ""} {
r := NewValReader(strings.NewReader(pad + num + rest))
var vals []string
for {
b, err := r.Next()
if err == io.EOF {
break
}
require.NoError(t, err)
vals = append(vals, strings.TrimSpace(string(b)))
}
want := []string{num}
if rest != "" {
want = append(want, "2")
}
require.Equal(t, want, vals[1:])
}
}
}
}

// Only a number can be cut short at the end of the read buffer and still
// parse, so a string, record, or array that exactly fills the buffer is
// returned without growing the buffer.
func TestValReaderFullBufferValue(t *testing.T) {
size := len(NewValReader(nil).buf)
for _, wrap := range [][2]string{{`"`, `"`}, {`{"a":"`, `"}`}, {`["`, `"]`}} {
val := wrap[0] + strings.Repeat("x", size-len(wrap[0])-len(wrap[1])) + wrap[1]
r := NewValReader(strings.NewReader(val + "\n1\n"))
b, err := r.Next()
require.NoError(t, err)
require.Equal(t, val, string(b))
require.Equal(t, size, len(r.buf))
b, err = r.Next()
require.NoError(t, err)
require.Equal(t, "1", strings.TrimSpace(string(b)))
}
}
16 changes: 16 additions & 0 deletions sio/jsonio/ztests/number-buffer-boundary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Bare numbers that straddle a boundary of the JSON reader's 512 KiB input
# buffer are read whole, not split into two values or rejected as invalid,
# from stdin and from a file.

script: |
seq 999999 | super -s -c 'aggregate count(), sum(this)' -
echo ===
seq 999999 | sed 's/$/.5/' > floats.json
super -s -c 'aggregate count(), sum(this)' floats.json

outputs:
- name: stdout
data: |
{count:999999,sum:499999500000}
===
{count:999999,sum:4.999999999995e+11}
Loading