From 2eb033c56842bf95ea19c057d9b29ee9233b0a5b Mon Sep 17 00:00:00 2001
From: Joao Pedro Silva <jp.silva15@gmail.com>
Date: Wed, 13 Dec 2023 15:01:11 +0000
Subject: [PATCH] FIX: Prevent panic on parameter evaluation

Signed-off-by: Joao Pedro Silva <jp.silva15@gmail.com>
---
 pkg/reconciler/pipelinerun/pipelinerun_test.go | 4 ++++
 pkg/reconciler/pipelinerun/resources/apply.go  | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/pkg/reconciler/pipelinerun/pipelinerun_test.go b/pkg/reconciler/pipelinerun/pipelinerun_test.go
index 85c78191d30..734e5335d8e 100644
--- a/pkg/reconciler/pipelinerun/pipelinerun_test.go
+++ b/pkg/reconciler/pipelinerun/pipelinerun_test.go
@@ -296,6 +296,8 @@ spec:
       value: $(context.pipeline.name)
     - name: contextRetriesParam
       value: $(context.pipelineTask.retries)
+    - name: param-not-found
+      value: $(params.notfound)
     retries: 5
     taskRef:
       name: unit-test-task
@@ -385,6 +387,8 @@ spec:
     value: test-pipeline
   - name: contextRetriesParam
     value: "5"
+  - name: param-not-found
+    value: $(params.notfound)
   retries: 5
   serviceAccountName: test-sa
   taskRef:
diff --git a/pkg/reconciler/pipelinerun/resources/apply.go b/pkg/reconciler/pipelinerun/resources/apply.go
index b76361b19c9..796baf206e3 100644
--- a/pkg/reconciler/pipelinerun/resources/apply.go
+++ b/pkg/reconciler/pipelinerun/resources/apply.go
@@ -160,7 +160,7 @@ func filterMatrixContextVar(params v1.Params) v1.Params {
 				// tasks.<pipelineTaskName>.matrix.length
 				// tasks.<pipelineTaskName>.matrix.<resultName>.length
 				subExpressions := strings.Split(expression, ".")
-				if subExpressions[2] == "matrix" && subExpressions[len(subExpressions)-1] == "length" {
+				if len(subExpressions) >= 4 && subExpressions[2] == "matrix" && subExpressions[len(subExpressions)-1] == "length" {
 					filteredParams = append(filteredParams, param)
 				}
 			}