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

Catch invalid literals created from expression factories #810

Merged
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
5 changes: 4 additions & 1 deletion checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package checker

import (
"fmt"
"reflect"

"github.com/google/cel-go/common"
"github.com/google/cel-go/common/ast"
Expand Down Expand Up @@ -78,6 +79,8 @@ func (c *checker) check(e ast.Expr) {
case types.BoolType, types.BytesType, types.DoubleType, types.IntType,
types.NullType, types.StringType, types.UintType:
c.setType(e, literal.Type().(*types.Type))
default:
c.errors.unexpectedASTType(e.ID(), c.location(e), "literal", literal.Type().TypeName())
}
case ast.IdentKind:
c.checkIdent(e)
Expand All @@ -94,7 +97,7 @@ func (c *checker) check(e ast.Expr) {
case ast.ComprehensionKind:
c.checkComprehension(e)
default:
c.errors.unexpectedASTType(e.ID(), c.location(e), e)
c.errors.unexpectedASTType(e.ID(), c.location(e), "unspecified", reflect.TypeOf(e).Name())
}
}

Expand Down
19 changes: 19 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/google/cel-go/common"
"github.com/google/cel-go/common/ast"
"github.com/google/cel-go/common/containers"
"github.com/google/cel-go/common/decls"
"github.com/google/cel-go/common/stdlib"
Expand Down Expand Up @@ -2542,6 +2544,23 @@ func TestCheckErrorData(t *testing.T) {
}
}

func TestCheckInvalidLiteral(t *testing.T) {
fac := ast.NewExprFactory()
durLiteral := fac.NewLiteral(1, types.Duration{Duration: time.Second})
// This is not valid syntax, just for illustration purposes.
src := common.NewTextSource(`1s`)
parsed := ast.NewAST(durLiteral, ast.NewSourceInfo(src))
reg := newTestRegistry(t)
env, err := NewEnv(containers.DefaultContainer, reg)
if err != nil {
t.Fatalf("NewEnv(cont, reg) failed: %v", err)
}
_, iss := Check(parsed, src, env)
if !strings.Contains(iss.ToDisplayString(), "unexpected literal type") {
t.Errorf("got %s, wanted 'unexpected literal type'", iss.ToDisplayString())
}
}

func testFunction(t testing.TB, name string, opts ...decls.FunctionOpt) *decls.FunctionDecl {
t.Helper()
fn, err := decls.NewFunction(name, opts...)
Expand Down
6 changes: 2 additions & 4 deletions checker/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package checker

import (
"reflect"

"github.com/google/cel-go/common"
"github.com/google/cel-go/common/ast"
"github.com/google/cel-go/common/types"
Expand Down Expand Up @@ -85,6 +83,6 @@ func (e *typeErrors) unexpectedFailedResolution(id int64, l common.Location, typ
e.errs.ReportErrorAtID(id, l, "unexpected failed resolution of '%s'", typeName)
}

func (e *typeErrors) unexpectedASTType(id int64, l common.Location, ex ast.Expr) {
e.errs.ReportErrorAtID(id, l, "unrecognized ast type: %v", reflect.TypeOf(ex))
func (e *typeErrors) unexpectedASTType(id int64, l common.Location, kind, typeName string) {
e.errs.ReportErrorAtID(id, l, "unexpected %s type: %v", kind, typeName)
}