Skip to content

Commit 1750dc9

Browse files
committed
Add bounds check for slice operations
1 parent 536f456 commit 1750dc9

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v1.4.1
4+
* Added bounds check for slice operations `foo[0:5]`
5+
36
## v1.4.0
47
* Added option to allow using undefined variables.
58
* Fixed getting default values out of maps.

expr_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -769,10 +769,26 @@ func TestExpr(t *testing.T) {
769769
`Variadic("empty")`,
770770
[]int{},
771771
},
772+
{
773+
`String[:]`,
774+
"string",
775+
},
772776
{
773777
`String[:3]`,
774778
"str",
775779
},
780+
{
781+
`String[:9]`,
782+
"string",
783+
},
784+
{
785+
`String[3:9]`,
786+
"ing",
787+
},
788+
{
789+
`String[7:9]`,
790+
"",
791+
},
776792
}
777793

778794
for _, tt := range tests {

vm/runtime.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,20 @@ func fetch(from interface{}, i interface{}) interface{} {
5757

5858
func slice(array, from, to interface{}) interface{} {
5959
v := reflect.ValueOf(array)
60-
switch v.Kind() {
6160

61+
switch v.Kind() {
6262
case reflect.Array, reflect.Slice, reflect.String:
63-
value := v.Slice(toInt(from), toInt(to))
63+
length := v.Len()
64+
a, b := toInt(from), toInt(to)
65+
66+
if b > length {
67+
b = length
68+
}
69+
if a > b {
70+
a = b
71+
}
72+
73+
value := v.Slice(a, b)
6474
if value.IsValid() && value.CanInterface() {
6575
return value.Interface()
6676
}

0 commit comments

Comments
 (0)