diff --git a/sio/jsonio/valreader.go b/sio/jsonio/valreader.go index e094797ab..0fd52dd3c 100644 --- a/sio/jsonio/valreader.go +++ b/sio/jsonio/valreader.go @@ -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 @@ -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()) } diff --git a/sio/jsonio/valreader_test.go b/sio/jsonio/valreader_test.go new file mode 100644 index 000000000..654c4eb5f --- /dev/null +++ b/sio/jsonio/valreader_test.go @@ -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))) + } +} diff --git a/sio/jsonio/ztests/number-buffer-boundary.yaml b/sio/jsonio/ztests/number-buffer-boundary.yaml new file mode 100644 index 000000000..397a71847 --- /dev/null +++ b/sio/jsonio/ztests/number-buffer-boundary.yaml @@ -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}