Fix segfault in model.Tree(procs)

Fixes crash in model.Tree(procs) which occurs when the list of procs
passed, isn't in the expected order. E.g. if the first element doesn't
have `PPID == 0`, it would crash writing to the uninitialized
`parent.Children`.
This commit is contained in:
Mikkel Oscar Lyderik Larsen 2017-04-30 17:36:55 +02:00
parent a540f8fa00
commit 83b81769df
No known key found for this signature in database
GPG key ID: 50AD98B2A0D8D4EF

View file

@ -46,14 +46,15 @@ func Tree(procs []*Proc) []*Proc {
var ( var (
nodes []*Proc nodes []*Proc
parent *Proc parent *Proc
children []*Proc
) )
for _, proc := range procs { for _, proc := range procs {
if proc.PPID == 0 { if proc.PPID == 0 {
nodes = append(nodes, proc) nodes = append(nodes, proc)
parent = proc parent = proc
continue parent.Children = children
} else { } else {
parent.Children = append(parent.Children, proc) children = append(children, proc)
} }
} }
return nodes return nodes