From a970145c95b9cc072905ca4f64c0640736aeff1f Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Tue, 22 Mar 2022 19:05:36 +0100 Subject: [PATCH] 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> --- pkg/exprparser/interpreter.go | 3 +++ pkg/exprparser/interpreter_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/pkg/exprparser/interpreter.go b/pkg/exprparser/interpreter.go index 81eec07..787bbda 100644 --- a/pkg/exprparser/interpreter.go +++ b/pkg/exprparser/interpreter.go @@ -170,6 +170,9 @@ func (impl *interperterImpl) evaluateIndexAccess(indexAccessNode *actionlint.Ind case reflect.Int: switch leftValue.Kind() { case reflect.Slice: + if rightValue.Int() < 0 || rightValue.Int() >= int64(leftValue.Len()) { + return nil, nil + } return leftValue.Index(int(rightValue.Int())).Interface(), nil default: return nil, fmt.Errorf("Unable to index on non-slice value: %s", leftValue.Kind()) diff --git a/pkg/exprparser/interpreter_test.go b/pkg/exprparser/interpreter_test.go index 32a94e4..83078d6 100644 --- a/pkg/exprparser/interpreter_test.go +++ b/pkg/exprparser/interpreter_test.go @@ -50,6 +50,10 @@ func TestOperators(t *testing.T) { {"github.action[0]", nil, "string-index", "Unable to index on non-slice value: string"}, {"fromJSON('[0,1]')[1]", 1.0, "array-index", ""}, {"(github.event.commits.*.author.username)[0]", "someone", "array-index-0", ""}, + {"fromJSON('[0,1]')[2]", nil, "array-index-out-of-bounds-0", ""}, + {"fromJSON('[0,1]')[34553]", nil, "array-index-out-of-bounds-1", ""}, + {"fromJSON('[0,1]')[-1]", nil, "array-index-out-of-bounds-2", ""}, + {"fromJSON('[0,1]')[-34553]", nil, "array-index-out-of-bounds-3", ""}, {"!true", false, "not", ""}, {"1 < 2", true, "less-than", ""}, {`'b' <= 'a'`, false, "less-than-or-equal", ""},