Skip to content

Commit f1c65fa

Browse files
diegommmantonmedv
andauthored
Fix runtime error not allowing to slice unaddressable array type (#803)
* fix runtime error not allowing to slice unaddressable array type * simplify logic and move test * remove dead code --------- Signed-off-by: Anton Medvedev <[email protected]> Co-authored-by: Anton Medvedev <[email protected]>
1 parent 85d0be8 commit f1c65fa

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

expr_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,23 @@ func TestMemoryBudget(t *testing.T) {
28462846
}
28472847
}
28482848

2849+
func TestIssue802(t *testing.T) {
2850+
prog, err := expr.Compile(`arr[1:2][0]`)
2851+
if err != nil {
2852+
t.Fatalf("error compiling program: %v", err)
2853+
}
2854+
val, err := expr.Run(prog, map[string]any{
2855+
"arr": [5]int{0, 1, 2, 3, 4},
2856+
})
2857+
if err != nil {
2858+
t.Fatalf("error running program: %v", err)
2859+
}
2860+
valInt, ok := val.(int)
2861+
if !ok || valInt != 1 {
2862+
t.Fatalf("invalid result, expected 1, got %v", val)
2863+
}
2864+
}
2865+
28492866
func TestIssue807(t *testing.T) {
28502867
type MyStruct struct {
28512868
nonExported string

vm/runtime/runtime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func Slice(array, from, to any) any {
166166
if a > b {
167167
a = b
168168
}
169+
if v.Kind() == reflect.Array && !v.CanAddr() {
170+
newValue := reflect.New(v.Type()).Elem()
171+
newValue.Set(v)
172+
v = newValue
173+
}
169174
value := v.Slice(a, b)
170175
if value.IsValid() {
171176
return value.Interface()

0 commit comments

Comments
 (0)