routing: fix slice mutation bug that could result in an infinite loop

This commit fixes a pretty nasty unnoticed bug within the main
k-shortest paths algorithm loop. After a new candidate path is found,
the rootPath (the path up to the pivot node) and the spurPath (the
_new_ path after the pivot node) are to be combined into a new candiate
shortest path. The prior logic simply appended the spurPath onto the
end of the rootPath to create a slice. However, if the case that the
currnet rootPath is really a sub-path in a larger slice, then this will
mutate the underlying slice.

This bug would manifest when doing path finding and cause an infinite
loop as the slice kept growing with new spurPaths, causing the loop to
never terminate. We remedy this bug by properly create a new backing
slice, and adding the elements to them rather than incorrectly mutating
an underlying slice.
This commit is contained in:
Olaoluwa Osuntokun 2017-04-13 14:48:11 -07:00
parent a4e26eaa4a
commit 5442e42cc1
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
1 changed files with 8 additions and 5 deletions

View File

@ -530,14 +530,17 @@ func findPaths(graph *channeldb.ChannelGraph, source *channeldb.LightningNode,
// Create the new combined path by concatenating the
// rootPath to the spurPath.
newPath := append(rootPath, spurPath...)
newPathLen := len(rootPath) + len(spurPath)
newPath := path{
hops: make([]*ChannelHop, 0, newPathLen),
dist: newPathLen,
}
newPath.hops = append(newPath.hops, rootPath...)
newPath.hops = append(newPath.hops, spurPath...)
// We'll now add this newPath to the heap of candidate
// shortest paths.
heap.Push(&candidatePaths, path{
dist: len(newPath),
hops: newPath,
})
heap.Push(&candidatePaths, newPath)
}
// If our min-heap of candidate paths is empty, then we can