Skip to content

Commit a419bfc

Browse files
authored
fix slice low/high index shadowing variables in vm (#394)
1 parent dfcfd66 commit a419bfc

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

vm.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ func (v *VM) run() {
376376

377377
var lowIdx int64
378378
if low != UndefinedValue {
379-
if low, ok := low.(*Int); ok {
380-
lowIdx = low.Value
379+
if lowInt, ok := low.(*Int); ok {
380+
lowIdx = lowInt.Value
381381
} else {
382382
v.err = fmt.Errorf("invalid slice index type: %s",
383383
low.TypeName())
@@ -391,8 +391,8 @@ func (v *VM) run() {
391391
var highIdx int64
392392
if high == UndefinedValue {
393393
highIdx = numElements
394-
} else if high, ok := high.(*Int); ok {
395-
highIdx = high.Value
394+
} else if highInt, ok := high.(*Int); ok {
395+
highIdx = highInt.Value
396396
} else {
397397
v.err = fmt.Errorf("invalid slice index type: %s",
398398
high.TypeName())
@@ -428,8 +428,8 @@ func (v *VM) run() {
428428
var highIdx int64
429429
if high == UndefinedValue {
430430
highIdx = numElements
431-
} else if high, ok := high.(*Int); ok {
432-
highIdx = high.Value
431+
} else if highInt, ok := high.(*Int); ok {
432+
highIdx = highInt.Value
433433
} else {
434434
v.err = fmt.Errorf("invalid slice index type: %s",
435435
high.TypeName())
@@ -465,8 +465,8 @@ func (v *VM) run() {
465465
var highIdx int64
466466
if high == UndefinedValue {
467467
highIdx = numElements
468-
} else if high, ok := high.(*Int); ok {
469-
highIdx = high.Value
468+
} else if highInt, ok := high.(*Int); ok {
469+
highIdx = highInt.Value
470470
} else {
471471
v.err = fmt.Errorf("invalid slice index type: %s",
472472
high.TypeName())
@@ -502,8 +502,8 @@ func (v *VM) run() {
502502
var highIdx int64
503503
if high == UndefinedValue {
504504
highIdx = numElements
505-
} else if high, ok := high.(*Int); ok {
506-
highIdx = high.Value
505+
} else if highInt, ok := high.(*Int); ok {
506+
highIdx = highInt.Value
507507
} else {
508508
v.err = fmt.Errorf("invalid slice index type: %s",
509509
high.TypeName())

vm_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,15 @@ export func() {
10901090
b := 5
10911091
return b + "foo"
10921092
}`), "Runtime Error: invalid operation: int + string\n\tat mod2:4:9")
1093+
1094+
expectError(t, `a := [1, 2, 3]; b := a[:"invalid"];`, nil,
1095+
"Runtime Error: invalid slice index type: string")
1096+
expectError(t, `a := immutable([4, 5, 6]); b := a[:false];`, nil,
1097+
"Runtime Error: invalid slice index type: bool")
1098+
expectError(t, `a := "hello"; b := a[:1.23];`, nil,
1099+
"Runtime Error: invalid slice index type: float")
1100+
expectError(t, `a := bytes("world"); b := a[:time(1)];`, nil,
1101+
"Runtime Error: invalid slice index type: time")
10931102
}
10941103

10951104
func TestVMErrorUnwrap(t *testing.T) {

0 commit comments

Comments
 (0)