Skip to content

Commit aa3560b

Browse files
committed
Fix slice out of bound errors
1 parent 4810a47 commit aa3560b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

expr_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1761,3 +1761,23 @@ func TestIssue401(t *testing.T) {
17611761
require.NoError(t, err, "run error")
17621762
require.Equal(t, 0.5, output)
17631763
}
1764+
1765+
func TestEval_slices_out_of_bound(t *testing.T) {
1766+
tests := []struct {
1767+
code string
1768+
want interface{}
1769+
}{
1770+
{"[1, 2, 3][:99]", []interface{}{1, 2, 3}},
1771+
{"[1, 2, 3][99:]", []interface{}{}},
1772+
{"[1, 2, 3][:-99]", []interface{}{}},
1773+
{"[1, 2, 3][-99:]", []interface{}{1, 2, 3}},
1774+
}
1775+
1776+
for _, tt := range tests {
1777+
t.Run(tt.code, func(t *testing.T) {
1778+
got, err := expr.Eval(tt.code, nil)
1779+
require.NoError(t, err, "eval error: "+tt.code)
1780+
assert.Equal(t, tt.want, got, "eval: "+tt.code)
1781+
})
1782+
}
1783+
}

vm/runtime/runtime.go

+6
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,15 @@ func Slice(array, from, to interface{}) interface{} {
183183
if a < 0 {
184184
a = length + a
185185
}
186+
if a < 0 {
187+
a = 0
188+
}
186189
if b < 0 {
187190
b = length + b
188191
}
192+
if b < 0 {
193+
b = 0
194+
}
189195
if b > length {
190196
b = length
191197
}

0 commit comments

Comments
 (0)