Skip to content

Commit 2269d3d

Browse files
authored
Merge pull request #922 from roidelapluie/roidelapluie/fix-textparse-empty-braces-panic
expfmt: fix nil pointer panic when parsing empty braces "{}"
2 parents 56fe395 + a1600af commit 2269d3d

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

expfmt/text_parse.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ func (p *TextParser) startLabelName() stateFn {
339339
return nil // Unexpected end of input.
340340
}
341341
if p.currentByte == '}' {
342+
if p.currentMF == nil {
343+
// The closing brace was reached before any metric name was read,
344+
// e.g. for the input "{}". There is no metric to attach labels to,
345+
// so this is a malformed exposition. This mirrors the guard in
346+
// startLabelValue. currentMF (not currentMetric) is checked because
347+
// reset only clears currentMF between parses.
348+
p.parseError("invalid metric name")
349+
p.currentLabelPairs = nil
350+
return nil
351+
}
342352
p.currentMetric.Label = append(p.currentMetric.Label, p.currentLabelPairs...)
343353
p.currentLabelPairs = nil
344354
if p.skipBlankTab(); p.err != nil {

expfmt/text_parse_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,12 @@ request_duration_microseconds_count 2693
13321332
`,
13331333
errUTF8: `text format parsing error in line 5: negative bucket population for histogram "request_duration_microseconds"`,
13341334
},
1335+
// 47: Empty braces with no metric name (regression test for a nil
1336+
// pointer dereference in startLabelName).
1337+
{
1338+
in: `{} 3.14`,
1339+
errUTF8: "text format parsing error in line 1: invalid metric name",
1340+
},
13351341
}
13361342
for i, scenario := range scenarios {
13371343
parser.scheme = model.UTF8Validation

0 commit comments

Comments
 (0)