Skip to content

Commit

Permalink
fix slice low/high index shadowing variables in vm (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipsusila authored Sep 26, 2022
1 parent dfcfd66 commit a419bfc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 10 additions & 10 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ func (v *VM) run() {

var lowIdx int64
if low != UndefinedValue {
if low, ok := low.(*Int); ok {
lowIdx = low.Value
if lowInt, ok := low.(*Int); ok {
lowIdx = lowInt.Value
} else {
v.err = fmt.Errorf("invalid slice index type: %s",
low.TypeName())
Expand All @@ -391,8 +391,8 @@ func (v *VM) run() {
var highIdx int64
if high == UndefinedValue {
highIdx = numElements
} else if high, ok := high.(*Int); ok {
highIdx = high.Value
} else if highInt, ok := high.(*Int); ok {
highIdx = highInt.Value
} else {
v.err = fmt.Errorf("invalid slice index type: %s",
high.TypeName())
Expand Down Expand Up @@ -428,8 +428,8 @@ func (v *VM) run() {
var highIdx int64
if high == UndefinedValue {
highIdx = numElements
} else if high, ok := high.(*Int); ok {
highIdx = high.Value
} else if highInt, ok := high.(*Int); ok {
highIdx = highInt.Value
} else {
v.err = fmt.Errorf("invalid slice index type: %s",
high.TypeName())
Expand Down Expand Up @@ -465,8 +465,8 @@ func (v *VM) run() {
var highIdx int64
if high == UndefinedValue {
highIdx = numElements
} else if high, ok := high.(*Int); ok {
highIdx = high.Value
} else if highInt, ok := high.(*Int); ok {
highIdx = highInt.Value
} else {
v.err = fmt.Errorf("invalid slice index type: %s",
high.TypeName())
Expand Down Expand Up @@ -502,8 +502,8 @@ func (v *VM) run() {
var highIdx int64
if high == UndefinedValue {
highIdx = numElements
} else if high, ok := high.(*Int); ok {
highIdx = high.Value
} else if highInt, ok := high.(*Int); ok {
highIdx = highInt.Value
} else {
v.err = fmt.Errorf("invalid slice index type: %s",
high.TypeName())
Expand Down
9 changes: 9 additions & 0 deletions vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,15 @@ export func() {
b := 5
return b + "foo"
}`), "Runtime Error: invalid operation: int + string\n\tat mod2:4:9")

expectError(t, `a := [1, 2, 3]; b := a[:"invalid"];`, nil,
"Runtime Error: invalid slice index type: string")
expectError(t, `a := immutable([4, 5, 6]); b := a[:false];`, nil,
"Runtime Error: invalid slice index type: bool")
expectError(t, `a := "hello"; b := a[:1.23];`, nil,
"Runtime Error: invalid slice index type: float")
expectError(t, `a := bytes("world"); b := a[:time(1)];`, nil,
"Runtime Error: invalid slice index type: time")
}

func TestVMErrorUnwrap(t *testing.T) {
Expand Down

0 comments on commit a419bfc

Please sign in to comment.