google / cel-go

Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)
https://cel.dev
Apache License 2.0
2.31k stars 225 forks source link

IndexOf in the ext pkg returns an error when it runs on an empty string #1051

Closed dmateusp closed 1 month ago

dmateusp commented 1 month ago

Describe the bug

"".indexOf("foo") returns index out of range: 0 instead of -1 when it runs against an empty string

To Reproduce Check which components this affects:

Sample expression and input that reproduces the issue:

"".indexOf("foo")

Test setup:

env, err := cel.NewEnv(ext.Strings())
if err != nil {
    t.Fatalf("Failed to create CEL env: %v", err)
}

celAST, compileIssues := env.Compile(`"".indexOf("foo")`)
assert.NotNil(t, celAST)
assert.Equal(t, "", compileIssues.String())

checkedExpr, err := cel.AstToCheckedExpr(celAST)
assert.Nil(t, err)
assert.NotNil(t, checkedExpr)

program, err := env.Program(celAST)
assert.NoError(t, err)
assert.NotNil(t, program)

_, _, err = program.Eval(map[string]interface{}{})
assert.NoError(t, err)

Expected behavior Returns -1 as it does when it does not find the substring

Additional context We use optionals, and we often do myValue.orValue("") to run conditionals on strings, but here doing myValue.orValue("").indexOf throws an error unexpectedly