Skip to content

Commit

Permalink
fix: plugins/parsers/influx: avoid ParseError.Error panic (influxdata…
Browse files Browse the repository at this point in the history
  • Loading branch information
rogpeppe authored Sep 30, 2020
1 parent 144228b commit af2e868
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugins/parsers/influx/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type ParseError struct {

func (e *ParseError) Error() string {
buffer := e.buf[e.LineOffset:]
eol := strings.IndexAny(buffer, "\r\n")
eol := strings.IndexAny(buffer, "\n")
if eol >= 0 {
buffer = buffer[:eol]
buffer = strings.TrimSuffix(buffer[:eol], "\r")
}
if len(buffer) > maxErrorBufferSize {
startEllipsis := true
Expand Down
15 changes: 15 additions & 0 deletions plugins/parsers/influx/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,18 @@ func TestSeriesParser(t *testing.T) {
buf: "cpu,a=",
},
},
{
name: "error with carriage return in long line",
input: []byte("cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b"),
metrics: []telegraf.Metric{},
err: &ParseError{
Offset: 1031,
LineNumber: 1,
Column: 1032,
msg: "parse error",
buf: "cpu,a=" + strings.Repeat("x", maxErrorBufferSize) + "\rcd,b",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -762,6 +774,9 @@ func TestSeriesParser(t *testing.T) {

metrics, err := parser.Parse(tt.input)
require.Equal(t, tt.err, err)
if err != nil {
require.Equal(t, tt.err.Error(), err.Error())
}

require.Equal(t, len(tt.metrics), len(metrics))
for i, expected := range tt.metrics {
Expand Down

0 comments on commit af2e868

Please sign in to comment.