Skip to content

Commit

Permalink
Fix panic if module by prefix is not found in Find() (#252)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sengleung authored Oct 26, 2023
1 parent 944052f commit d57549b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/yang/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d57549b

Please sign in to comment.