app/vmselect/promql: fix panic on sum(aggr_over_time(...)) with incorrect number of args

This commit is contained in:
Aliaksandr Valialkin 2020-01-15 16:26:02 +02:00
parent 7d429e2806
commit ce0b602405
2 changed files with 8 additions and 1 deletions

View File

@ -329,6 +329,10 @@ func tryGetArgRollupFuncWithMetricExpr(ae *metricsql.AggrFuncExpr) (*metricsql.F
return nil, nil return nil, nil
} }
rollupArgIdx := getRollupArgIdx(fe.Name) rollupArgIdx := getRollupArgIdx(fe.Name)
if rollupArgIdx >= len(fe.Args) {
// Incorrect number of args for rollup func.
return nil, nil
}
arg := fe.Args[rollupArgIdx] arg := fe.Args[rollupArgIdx]
if me, ok := arg.(*metricsql.MetricExpr); ok { if me, ok := arg.(*metricsql.MetricExpr); ok {
if me.IsEmpty() { if me.IsEmpty() {
@ -366,7 +370,7 @@ func evalRollupFuncArgs(ec *EvalConfig, fe *metricsql.FuncExpr) ([]interface{},
var re *metricsql.RollupExpr var re *metricsql.RollupExpr
rollupArgIdx := getRollupArgIdx(fe.Name) rollupArgIdx := getRollupArgIdx(fe.Name)
if len(fe.Args) <= rollupArgIdx { 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)) args := make([]interface{}, len(fe.Args))
for i, arg := range fe.Args { for i, arg := range fe.Args {

View File

@ -5304,6 +5304,9 @@ func TestExecError(t *testing.T) {
f(`aggr_over_time()`) f(`aggr_over_time()`)
f(`aggr_over_time(foo)`) f(`aggr_over_time(foo)`)
f(`aggr_over_time("foo", bar, 1)`) 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()`)
f(`hoeffding_bound_lower(1)`) f(`hoeffding_bound_lower(1)`)
f(`hoeffding_bound_lower(0.99, foo, 1)`) f(`hoeffding_bound_lower(0.99, foo, 1)`)