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 {