Skip to content

Fix runtime error not allowing to slice unaddressable array type #803

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2765,3 +2765,20 @@ func TestMemoryBudget(t *testing.T) {
})
}
}

func TestIssue802(t *testing.T) {
prog, err := expr.Compile(`arr[1:2][0]`)
if err != nil {
t.Fatalf("error compiling program: %v", err)
}
val, err := expr.Run(prog, map[string]any{
"arr": [5]int{0, 1, 2, 3, 4},
})
if err != nil {
t.Fatalf("error running program: %v", err)
}
valInt, ok := val.(int)
if !ok || valInt != 1 {
t.Fatalf("invalid result, expected 1, got %v", val)
}
}
5 changes: 5 additions & 0 deletions vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ func Slice(array, from, to any) any {
if a > b {
a = b
}
if v.Kind() == reflect.Array && !v.CanAddr() {
newValue := reflect.New(v.Type()).Elem()
newValue.Set(v)
v = newValue
}
value := v.Slice(a, b)
if value.IsValid() {
return value.Interface()
Expand Down