Skip to content

Commit

Permalink
Add bounds check for slice operations
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Nov 13, 2019
1 parent 536f456 commit 1750dc9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v1.4.1
* Added bounds check for slice operations `foo[0:5]`

## v1.4.0
* Added option to allow using undefined variables.
* Fixed getting default values out of maps.
Expand Down
16 changes: 16 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,26 @@ func TestExpr(t *testing.T) {
`Variadic("empty")`,
[]int{},
},
{
`String[:]`,
"string",
},
{
`String[:3]`,
"str",
},
{
`String[:9]`,
"string",
},
{
`String[3:9]`,
"ing",
},
{
`String[7:9]`,
"",
},
}

for _, tt := range tests {
Expand Down
14 changes: 12 additions & 2 deletions vm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ func fetch(from interface{}, i interface{}) interface{} {

func slice(array, from, to interface{}) interface{} {
v := reflect.ValueOf(array)
switch v.Kind() {

switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
value := v.Slice(toInt(from), toInt(to))
length := v.Len()
a, b := toInt(from), toInt(to)

if b > length {
b = length
}
if a > b {
a = b
}

value := v.Slice(a, b)
if value.IsValid() && value.CanInterface() {
return value.Interface()
}
Expand Down

0 comments on commit 1750dc9

Please sign in to comment.