Skip to content

Commit a970145

Browse files
Fix: panic: reflect: slice index out of range (#1066)
* Fix: panic: reflect: slice index out of range * Update interpreter.go * [no ci] Return null for negative indexes * Add tests for index access Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 380bd58 commit a970145

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

pkg/exprparser/interpreter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ func (impl *interperterImpl) evaluateIndexAccess(indexAccessNode *actionlint.Ind
170170
case reflect.Int:
171171
switch leftValue.Kind() {
172172
case reflect.Slice:
173+
if rightValue.Int() < 0 || rightValue.Int() >= int64(leftValue.Len()) {
174+
return nil, nil
175+
}
173176
return leftValue.Index(int(rightValue.Int())).Interface(), nil
174177
default:
175178
return nil, fmt.Errorf("Unable to index on non-slice value: %s", leftValue.Kind())

pkg/exprparser/interpreter_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func TestOperators(t *testing.T) {
5050
{"github.action[0]", nil, "string-index", "Unable to index on non-slice value: string"},
5151
{"fromJSON('[0,1]')[1]", 1.0, "array-index", ""},
5252
{"(github.event.commits.*.author.username)[0]", "someone", "array-index-0", ""},
53+
{"fromJSON('[0,1]')[2]", nil, "array-index-out-of-bounds-0", ""},
54+
{"fromJSON('[0,1]')[34553]", nil, "array-index-out-of-bounds-1", ""},
55+
{"fromJSON('[0,1]')[-1]", nil, "array-index-out-of-bounds-2", ""},
56+
{"fromJSON('[0,1]')[-34553]", nil, "array-index-out-of-bounds-3", ""},
5357
{"!true", false, "not", ""},
5458
{"1 < 2", true, "less-than", ""},
5559
{`'b' <= 'a'`, false, "less-than-or-equal", ""},

0 commit comments

Comments
 (0)