Skip to content

Commit 13cd50c

Browse files
committed
Return error on NaN and Inf timestamps for OpenMetrics parser
Signed-off-by: cw9 <[email protected]>
1 parent 664b391 commit 13cd50c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pkg/textparse/openmetricsparse.go

+6
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
336336
if ts, err = parseFloat(yoloString(p.l.buf()[1:])); err != nil {
337337
return EntryInvalid, err
338338
}
339+
if math.IsNaN(ts) || math.IsInf(ts, 0) {
340+
return EntryInvalid, errors.New("invalid timestamp")
341+
}
339342
p.ts = int64(ts * 1000)
340343
switch t3 := p.nextToken(); t3 {
341344
case tLinebreak:
@@ -392,6 +395,9 @@ func (p *OpenMetricsParser) parseComment() error {
392395
if ts, err = parseFloat(yoloString(p.l.buf()[1:])); err != nil {
393396
return err
394397
}
398+
if math.IsNaN(ts) || math.IsInf(ts, 0) {
399+
return errors.New("invalid exemplar timestamp")
400+
}
395401
p.exemplarTs = int64(ts * 1000)
396402
switch t3 := p.nextToken(); t3 {
397403
case tLinebreak:

pkg/textparse/openmetricsparse_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,30 @@ func TestOpenMetricsParseErrors(t *testing.T) {
504504
input: `{b="c",} 1`,
505505
err: `"INVALID" "{" is not a valid start token`,
506506
},
507+
{
508+
input: `a 1 NaN`,
509+
err: `invalid timestamp`,
510+
},
511+
{
512+
input: `a 1 -Inf`,
513+
err: `invalid timestamp`,
514+
},
515+
{
516+
input: `a 1 Inf`,
517+
err: `invalid timestamp`,
518+
},
519+
{
520+
input: "# TYPE hhh histogram\nhhh_bucket{le=\"+Inf\"} 1 # {aa=\"bb\"} 4 NaN",
521+
err: `invalid exemplar timestamp`,
522+
},
523+
{
524+
input: "# TYPE hhh histogram\nhhh_bucket{le=\"+Inf\"} 1 # {aa=\"bb\"} 4 -Inf",
525+
err: `invalid exemplar timestamp`,
526+
},
527+
{
528+
input: "# TYPE hhh histogram\nhhh_bucket{le=\"+Inf\"} 1 # {aa=\"bb\"} 4 Inf",
529+
err: `invalid exemplar timestamp`,
530+
},
507531
}
508532

509533
for i, c := range cases {

0 commit comments

Comments
 (0)