From d57549bc16ba6cec59874b1526e8cbf6b4af8102 Mon Sep 17 00:00:00 2001 From: Seng Leung Date: Thu, 26 Oct 2023 02:39:56 +0100 Subject: [PATCH] Fix panic if module by prefix is not found in Find() (#252) Previously, if FindModuleByPrefix() returns nil in Find(), it would continue to call module() with nil, resulting in a nil pointer dereference panic when trying to call ParentNode() from RootNode(). The fix is to return an error if FindModuleByPrefix() returns nil. Fixes #251 --- pkg/yang/entry.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/yang/entry.go b/pkg/yang/entry.go index 97cce31..77d0964 100644 --- a/pkg/yang/entry.go +++ b/pkg/yang/entry.go @@ -1322,11 +1322,17 @@ func (e *Entry) Find(name string) *Entry { e = e.Parent } if prefix, _ := getPrefix(parts[0]); prefix != "" { - m := module(FindModuleByPrefix(contextNode, prefix)) - if m == nil { + mod := FindModuleByPrefix(contextNode, prefix) + if mod == nil { e.addError(fmt.Errorf("cannot find module giving prefix %q within context entry %q", prefix, e.Path())) return nil } + m := module(mod) + if m == nil { + e.addError(fmt.Errorf("cannot find which module %q belongs to within context entry %q", + mod.NName(), e.Path())) + return nil + } if m != e.Node.(*Module) { e = ToEntry(m) }