diff --git a/runtime/vm.go b/runtime/vm.go index 34ff212..cc1990b 100644 --- a/runtime/vm.go +++ b/runtime/vm.go @@ -644,7 +644,7 @@ func (v *VM) Run() error { case *objects.Array: numElements := int64(len(left.Value)) - if lowIdx < 0 || lowIdx >= numElements { + if lowIdx < 0 || lowIdx > numElements { return fmt.Errorf("index out of bounds: %d", lowIdx) } if highIdx < 0 { @@ -669,7 +669,7 @@ func (v *VM) Run() error { case *objects.ImmutableArray: numElements := int64(len(left.Value)) - if lowIdx < 0 || lowIdx >= numElements { + if lowIdx < 0 || lowIdx > numElements { return fmt.Errorf("index out of bounds: %d", lowIdx) } if highIdx < 0 { @@ -694,7 +694,7 @@ func (v *VM) Run() error { case *objects.String: numElements := int64(len(left.Value)) - if lowIdx < 0 || lowIdx >= numElements { + if lowIdx < 0 || lowIdx > numElements { return fmt.Errorf("index out of bounds: %d", lowIdx) } if highIdx < 0 { diff --git a/runtime/vm_string_test.go b/runtime/vm_string_test.go index d3e7985..76611ff 100644 --- a/runtime/vm_string_test.go +++ b/runtime/vm_string_test.go @@ -28,7 +28,7 @@ func TestString(t *testing.T) { expectError(t, fmt.Sprintf("%s[%d]", strStr, strLen)) // slice operator - for low := 0; low < strLen; low++ { + for low := 0; low <= strLen; low++ { for high := low; high <= strLen; high++ { expect(t, fmt.Sprintf("out = %s[%d:%d]", strStr, low, high), str[low:high]) expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", strStr, low, high), str[low:high])