From 6d8a427397115d8a38f872dbfaebd8efd0dd0983 Mon Sep 17 00:00:00 2001 From: "Shichu (Stuart) Zhu" Date: Tue, 18 Oct 2022 12:55:17 -0400 Subject: [PATCH] ytypes.UnmarshalSetRequest does not panic at nil input. (#737) * [fix] ygot.UnmarshalSetRequest panic at nil request. This function accesses the fields of req (req.Prefix, req.Delete etc). A input check is added to prevent nil pointer panic. * [test] Add a nil test case for UnmarshalSetRequest Add unit test to verify the function works when input request is nil. Co-authored-by: Wen Bo Li <50884368+wenovus@users.noreply.github.com> --- ytypes/gnmi.go | 4 +++- ytypes/gnmi_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ytypes/gnmi.go b/ytypes/gnmi.go index 5eea45830..52a463a3e 100644 --- a/ytypes/gnmi.go +++ b/ytypes/gnmi.go @@ -46,7 +46,9 @@ func UnmarshalNotifications(schema *Schema, ns []*gpb.Notification, opts ...Unma func UnmarshalSetRequest(schema *Schema, req *gpb.SetRequest, opts ...UnmarshalOpt) error { preferShadowPath := hasPreferShadowPath(opts) ignoreExtraFields := hasIgnoreExtraFields(opts) - + if req == nil { + return nil + } root := schema.Root node, nodeName, err := getOrCreateNode(schema.RootSchema(), root, req.Prefix, preferShadowPath) if err != nil { diff --git a/ytypes/gnmi_test.go b/ytypes/gnmi_test.go index 0a863d7fd..c5d7ef621 100644 --- a/ytypes/gnmi_test.go +++ b/ytypes/gnmi_test.go @@ -19,6 +19,15 @@ func TestUnmarshalSetRequest(t *testing.T) { want ygot.GoStruct wantErr bool }{{ + desc: "nil input", + inSchema: &Schema{ + Root: &ListElemStruct1{}, + SchemaTree: map[string]*yang.Entry{ + "ListElemStruct1": simpleSchema(), + }, + }, + want: &ListElemStruct1{}, + }, { desc: "updates to an empty struct", inSchema: &Schema{ Root: &ListElemStruct1{},