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

List access errors are ambiguous #683

Closed
dstephan1 opened this issue Apr 14, 2023 · 1 comment · Fixed by #693
Closed

List access errors are ambiguous #683

dstephan1 opened this issue Apr 14, 2023 · 1 comment · Fixed by #693

Comments

@dstephan1
Copy link

Describe the bug
If you have an expression like mylistundef[0] == 'hi', and 'mylistundef' is undefined, you get an ambiguous error "fails with no such attribute: id: 2, names: []".

To Reproduce
Check which components this affects: (not sure)

Sample expression and input that reproduces the issue:

mylistundef[0] == 'hi'

Test setup:

// test case in go
func testList() error {
	env, err := cel.NewEnv(
		cel.Variable("mylist", cel.ListType(cel.StringType)),
	)
	if err != nil {
		return err
	}
	ast, issues := env.Parse(`mylistundef[0] == 'hi'`)
	if err := issues.Err(); err != nil {
		return err
	}
	program, err := env.Program(ast)

	varmap := map[string]any{}
	varmap["mylist"] = []string{"hi"}
	val, _, err := program.Eval(varmap)
	if err != nil { // fails with no such attribute: id: 2, names: []
		return err
	}

	match, ok := val.Value().(bool)
	if !ok {
		return fmt.Errorf("type convert failed: type is %v", val.Type())
	}
	return nil
}

func TestList(t *testing.T) {
	if err := testList(); err != nil {
		t.Error(err)
	}
}

Expected behavior
There should be a clear error, such as "mylistundef is undefined"

@dstephan1
Copy link
Author

https://github.com/google/cel-go/blob/master/interpreter/attributes.go

Resolve returns the first error it sees. Which is dereference of the deepest variable. Ex: 'foo.bar.baz[0]' will error in this order: no such attr: 2 -> no such attr baz -> no such attr bar -> no such attr foo.

And what you end up with is a vague "no such attribute: id: 2".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant