Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nil safety checks for cel.Ast #784

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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