From 3bd64135b43de19575d3b012144a20e578f6f8e1 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Wed, 18 Dec 2024 11:12:07 -0500 Subject: [PATCH] Don't panic when decoding empty slice. --- pkg/types/ccipocr3/plugin_execute_types.go | 4 ++++ pkg/types/ccipocr3/plugin_execute_types_test.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/pkg/types/ccipocr3/plugin_execute_types.go b/pkg/types/ccipocr3/plugin_execute_types.go index 9ea1aefc..8b4b5ad3 100644 --- a/pkg/types/ccipocr3/plugin_execute_types.go +++ b/pkg/types/ccipocr3/plugin_execute_types.go @@ -33,6 +33,10 @@ func (eri ExecuteReportInfo) Encode() ([]byte, error) { // DecodeExecuteReportInfo is a version aware decode function for the execute // report info bytes. func DecodeExecuteReportInfo(data []byte) (ExecuteReportInfo, error) { + if len(data) == 0 { + return ExecuteReportInfo{}, nil + } + switch data[0] { case 1: var result ExecuteReportInfo diff --git a/pkg/types/ccipocr3/plugin_execute_types_test.go b/pkg/types/ccipocr3/plugin_execute_types_test.go index 974fd3b3..dffb438a 100644 --- a/pkg/types/ccipocr3/plugin_execute_types_test.go +++ b/pkg/types/ccipocr3/plugin_execute_types_test.go @@ -28,6 +28,13 @@ func TestDecodeExecuteReportInfo(t *testing.T) { _, err := DecodeExecuteReportInfo(data) require.ErrorContains(t, err, "object") // not super helpful... } + + // empty + { + ri, err := DecodeExecuteReportInfo(nil) + require.NoError(t, err) + require.Equal(t, ExecuteReportInfo{}, ri) + } } func TestExecuteReportInfo_EncodeDecode(t *testing.T) {