From 6efb4a8056e1b606a0a939972b593b0f69283c75 Mon Sep 17 00:00:00 2001 From: Cici Huang <8658046+cici37@users.noreply.github.com> Date: Thu, 10 Mar 2022 14:36:53 -0800 Subject: [PATCH] Add nil check and fix costTracker in contextEval (#507) * Return proper err for nil context * Fix costTracker in contextEval --- cel/program.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cel/program.go b/cel/program.go index 09909539..f0971607 100644 --- a/cel/program.go +++ b/cel/program.go @@ -293,6 +293,9 @@ func (p *prog) Eval(input interface{}) (v ref.Val, det *EvalDetails, err error) // ContextEval implements the Program interface. func (p *prog) ContextEval(ctx context.Context, input interface{}) (ref.Val, *EvalDetails, error) { + if ctx == nil { + return nil, nil, fmt.Errorf("context can not be nil") + } // Configure the input, making sure to wrap Activation inputs in the special ctxActivation which // exposes the #interrupted variable and manages rate-limited checks of the ctx.Done() state. var vars interpreter.Activation @@ -362,16 +365,20 @@ func (gen *progGen) Eval(input interface{}) (ref.Val, *EvalDetails, error) { // ContextEval implements the Program interface method. func (gen *progGen) ContextEval(ctx context.Context, input interface{}) (ref.Val, *EvalDetails, error) { + if ctx == nil { + return nil, nil, fmt.Errorf("context can not be nil") + } // The factory based Eval() differs from the standard evaluation model in that it generates a // new EvalState instance for each call to ensure that unique evaluations yield unique stateful // results. state := interpreter.NewEvalState() - det := &EvalDetails{state: state} + costTracker := &interpreter.CostTracker{} + det := &EvalDetails{state: state, costTracker: costTracker} // Generate a new instance of the interpretable using the factory configured during the call to // newProgram(). It is incredibly unlikely that the factory call will generate an error given // the factory test performed within the Program() call. - p, err := gen.factory(state, &interpreter.CostTracker{}) + p, err := gen.factory(state, costTracker) if err != nil { return nil, det, err }