Skip to content

Commit c215c03

Browse files
committed
Validate array.rindex offset before empty-array return
1 parent 44be2c3 commit c215c03

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

vibes/execution.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,18 +2400,21 @@ func arrayMember(array Value, property string) (Value, error) {
24002400
if len(args) < 1 || len(args) > 2 {
24012401
return NewNil(), fmt.Errorf("array.rindex expects value and optional offset")
24022402
}
2403-
arr := receiver.Array()
2404-
if len(arr) == 0 {
2405-
return NewNil(), nil
2406-
}
2407-
offset := len(arr) - 1
2403+
offset := -1
24082404
if len(args) == 2 {
24092405
n, err := valueToInt(args[1])
24102406
if err != nil || n < 0 {
24112407
return NewNil(), fmt.Errorf("array.rindex offset must be non-negative integer")
24122408
}
24132409
offset = n
24142410
}
2411+
arr := receiver.Array()
2412+
if len(arr) == 0 {
2413+
return NewNil(), nil
2414+
}
2415+
if offset < 0 {
2416+
offset = len(arr) - 1
2417+
}
24152418
if offset >= len(arr) {
24162419
offset = len(arr) - 1
24172420
}

vibes/runtime_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,11 @@ func TestMethodErrorHandling(t *testing.T) {
14731473
script: `def run() [1, 2, 3].rindex(2, 1, 0) end`,
14741474
errMsg: "expects value and optional offset",
14751475
},
1476+
{
1477+
name: "array.rindex validates offset on empty array",
1478+
script: `def run() [].rindex(1, -1) end`,
1479+
errMsg: "offset must be non-negative integer",
1480+
},
14761481
{
14771482
name: "array.count with argument and block",
14781483
script: `def run()

0 commit comments

Comments
 (0)