Skip to content

Commit

Permalink
ytypes.UnmarshalSetRequest does not panic at nil input. (#737)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
shichuzhu and wenovus authored Oct 18, 2022
1 parent 7e512ef commit 6d8a427
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ytypes/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions ytypes/gnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down

0 comments on commit 6d8a427

Please sign in to comment.