From ce0b602405be999770fd06e2c6f1452abfd67e68 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 15 Jan 2020 16:26:02 +0200 Subject: [PATCH] app/vmselect/promql: fix panic on `sum(aggr_over_time(...))` with incorrect number of args --- app/vmselect/promql/eval.go | 6 +++++- app/vmselect/promql/exec_test.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/vmselect/promql/eval.go b/app/vmselect/promql/eval.go index 761e1db4b6..b58c8fdb07 100644 --- a/app/vmselect/promql/eval.go +++ b/app/vmselect/promql/eval.go @@ -329,6 +329,10 @@ func tryGetArgRollupFuncWithMetricExpr(ae *metricsql.AggrFuncExpr) (*metricsql.F return nil, nil } rollupArgIdx := getRollupArgIdx(fe.Name) + if rollupArgIdx >= len(fe.Args) { + // Incorrect number of args for rollup func. + return nil, nil + } arg := fe.Args[rollupArgIdx] if me, ok := arg.(*metricsql.MetricExpr); ok { if me.IsEmpty() { @@ -366,7 +370,7 @@ func evalRollupFuncArgs(ec *EvalConfig, fe *metricsql.FuncExpr) ([]interface{}, var re *metricsql.RollupExpr rollupArgIdx := getRollupArgIdx(fe.Name) if len(fe.Args) <= rollupArgIdx { - return nil, nil, fmt.Errorf("expecting at least %d args to %q; got %d args; expr: %q", rollupArgIdx, fe.Name, len(fe.Args), fe.AppendString(nil)) + return nil, nil, fmt.Errorf("expecting at least %d args to %q; got %d args; expr: %q", rollupArgIdx+1, fe.Name, len(fe.Args), fe.AppendString(nil)) } args := make([]interface{}, len(fe.Args)) for i, arg := range fe.Args { diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 731f1e2af1..ea99f0c75a 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -5304,6 +5304,9 @@ func TestExecError(t *testing.T) { f(`aggr_over_time()`) f(`aggr_over_time(foo)`) f(`aggr_over_time("foo", bar, 1)`) + f(`sum(aggr_over_time())`) + f(`sum(aggr_over_time(foo))`) + f(`count(aggr_over_time("foo", bar, 1))`) f(`hoeffding_bound_lower()`) f(`hoeffding_bound_lower(1)`) f(`hoeffding_bound_lower(0.99, foo, 1)`)