From c66bfce8cfd71d0414264f7102ce60cb1bfa4c9b Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Fri, 23 Aug 2019 15:44:16 +0100 Subject: [PATCH] Don't panic on short input. (#5939) Fixes #5935 Signed-off-by: Brian Brazil --- pkg/textparse/openmetricsparse.go | 5 ++++- pkg/textparse/openmetricsparse_test.go | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/textparse/openmetricsparse.go b/pkg/textparse/openmetricsparse.go index a1b0d6977..853076f1d 100644 --- a/pkg/textparse/openmetricsparse.go +++ b/pkg/textparse/openmetricsparse.go @@ -44,7 +44,10 @@ func (l *openMetricsLexer) buf() []byte { } func (l *openMetricsLexer) cur() byte { - return l.b[l.i] + if l.i < len(l.b) { + return l.b[l.i] + } + return byte(' ') } // next advances the openMetricsLexer to the next character. diff --git a/pkg/textparse/openmetricsparse_test.go b/pkg/textparse/openmetricsparse_test.go index 018b4be88..f8c792588 100644 --- a/pkg/textparse/openmetricsparse_test.go +++ b/pkg/textparse/openmetricsparse_test.go @@ -419,6 +419,14 @@ func TestOMNullByteHandling(t *testing.T) { input: "a\x00{b=\"ddd\"} 1", err: "expected value after metric, got \"MNAME\"", }, + { + input: "#", + err: "\"INVALID\" \" \" is not a valid start token", + }, + { + input: "# H", + err: "\"INVALID\" \" \" is not a valid start token", + }, } for i, c := range cases {