fix: prevent invalid array access in aggregate expression

This commit fixes the evaluation of invalid expressions like
`sum(rate(`. Before that, it would trigger a panic in the PromQL engine
because it tried to access an index which is out of range.

The bug was probably introduced by 06d0b063ea.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
pull/16754/head
Simon Pasquier 1 week ago
parent f69c83f5f1
commit 71217a6e43
No known key found for this signature in database
GPG Key ID: 0190A66C0A10FC4F
  1. 5
      promql/parser/parse.go
  2. 5
      promql/parser/parse_test.go

@ -451,6 +451,11 @@ func (p *parser) newAggregateExpr(op Item, modifier, args Node) (ret *AggregateE
ret = modifier.(*AggregateExpr)
arguments := args.(Expressions)
if len(p.closingParens) == 0 {
// Prevents invalid array accesses.
// The error is already captured by the parser.
return
}
ret.PosRange = posrange.PositionRange{
Start: op.Pos,
End: p.closingParens[0],

@ -4540,6 +4540,11 @@ var testExpr = []struct {
PosRange: posrange.PositionRange{Start: 0, End: 20},
},
},
{
input: "sum(rate(",
fail: true,
errMsg: "unclosed left parenthesis",
},
}
func makeInt64Pointer(val int64) *int64 {

Loading…
Cancel
Save