fix: plugins/parsers/influx: avoid ParseError.Error panic (#8177)

This commit is contained in:
Roger Peppe 2020-09-30 18:26:00 +01:00 committed by GitHub
parent 3efec1a10c
commit 9ee87ab7c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -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

View File

@ -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) {
@ -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 {