fix slice low/high index shadowing variables in vm (#394)

This commit is contained in:
I Putu Susila 2022-09-26 14:19:00 +07:00 committed by GitHub
parent dfcfd6661c
commit a419bfc93a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

20
vm.go
View file

@ -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())
@ -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())
@ -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())
@ -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())
@ -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())

View file

@ -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) {