Skip to content

Commit

Permalink
Nil safety checks for cel.Ast (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones authored Jul 31, 2023
1 parent 965e9c8 commit 5359cfd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
18 changes: 15 additions & 3 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,25 @@ type Ast struct {

// Expr returns the proto serializable instance of the parsed/checked expression.
func (ast *Ast) Expr() *exprpb.Expr {
if ast == nil {
return nil
}
return ast.expr
}

// IsChecked returns whether the Ast value has been successfully type-checked.
func (ast *Ast) IsChecked() bool {
if ast == nil {
return false
}
return ast.typeMap != nil && len(ast.typeMap) > 0
}

// SourceInfo returns character offset and newline position information about expression elements.
func (ast *Ast) SourceInfo() *exprpb.SourceInfo {
if ast == nil {
return nil
}
return ast.info
}

Expand All @@ -65,9 +74,6 @@ func (ast *Ast) SourceInfo() *exprpb.SourceInfo {
//
// Deprecated: use OutputType
func (ast *Ast) ResultType() *exprpb.Type {
if !ast.IsChecked() {
return chkdecls.Dyn
}
out := ast.OutputType()
t, err := TypeToExprType(out)
if err != nil {
Expand All @@ -79,6 +85,9 @@ func (ast *Ast) ResultType() *exprpb.Type {
// OutputType returns the output type of the expression if the Ast has been type-checked, else
// returns cel.DynType as the parse step cannot infer types.
func (ast *Ast) OutputType() *Type {
if ast == nil {
return types.ErrorType
}
t, found := ast.typeMap[ast.expr.GetId()]
if !found {
return DynType
Expand All @@ -89,6 +98,9 @@ func (ast *Ast) OutputType() *Type {
// Source returns a view of the input used to create the Ast. This source may be complete or
// constructed from the SourceInfo.
func (ast *Ast) Source() Source {
if ast == nil {
return nil
}
return ast.source
}

Expand Down
19 changes: 19 additions & 0 deletions cel/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ import (
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)

func TestAstNil(t *testing.T) {
var ast *Ast
if ast.IsChecked() {
t.Error("ast.IsChecked() returned true for nil ast")
}
if ast.Expr() != nil {
t.Errorf("ast.Expr() got %v, wanted nil", ast.Expr())
}
if ast.SourceInfo() != nil {
t.Errorf("ast.SourceInfo() got %v, wanted nil", ast.SourceInfo())
}
if ast.OutputType() != types.ErrorType {
t.Errorf("ast.OutputType() got %v, wanted error type", ast.OutputType())
}
if ast.Source() != nil {
t.Errorf("ast.Source() got %v, wanted nil", ast.Source())
}
}

func TestIssuesNil(t *testing.T) {
var iss *Issues
iss = iss.Append(iss)
Expand Down

0 comments on commit 5359cfd

Please sign in to comment.