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>
This commit is contained in:
ChristopherHX 2022-03-22 19:05:36 +01:00 committed by GitHub
parent 380bd581b1
commit a970145c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

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

View File

@ -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", ""},