Skip to content

Commit

Permalink
routing: fix slice mutation bug that could result in an infinite loop
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Roasbeef committed Apr 13, 2017
1 parent a4e26ea commit 5442e42
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions routing/pathfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5442e42

Please sign in to comment.