Skip to content

Commit

Permalink
parse: handle lines up to 1MiB long
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Jan 2, 2022
1 parent 77c8785 commit fa5644c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const (
LevelFatal
)

// LineBufferSize is the longest we're willing to look for a newline in the input.
const LineBufferSize = 1 * 1024 * 1024 // 1 MiB

// InputSchema controls the interpretation of incoming log lines.
type InputSchema struct {
TimeKey string // The name of the key that holds the timestamp.
Expand Down Expand Up @@ -193,6 +196,7 @@ func runJQ(jq *gojq.Code, l *line) (bool, error) {
// on the reader, are returned.
func ReadLog(r io.Reader, w io.Writer, ins *InputSchema, outs *OutputSchema, jq *gojq.Code) (Summary, error) {
s := bufio.NewScanner(r)
s.Buffer(make([]byte, 0, LineBufferSize), LineBufferSize)
var l line
outs.state = State{
lastFields: make(map[string][]byte),
Expand Down
8 changes: 8 additions & 0 deletions pkg/parse/parse_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package parse

import (
"bufio"
"bytes"
"errors"
"testing"

"github.com/logrusorgru/aurora/v3"
Expand Down Expand Up @@ -38,6 +40,12 @@ func FuzzReadLog(f *testing.F) {
}
outbuf := new(bytes.Buffer)
if _, err := ReadLog(inbuf, outbuf, ins, outs, jq); err != nil {
if errors.Is(err, bufio.ErrTooLong) {
// This is a known limit and the fuzzer likes to produce very long
// garbage lines. The tests convinced me to increase this limit,
// but it has to be limited somewhere.
t.SkipNow()
}
t.Fatal(err)
}
if got, want := bytes.Count(outbuf.Bytes(), []byte("\n")), bytes.Count(in, []byte("\n")); got < want {
Expand Down

Large diffs are not rendered by default.

0 comments on commit fa5644c

Please sign in to comment.