rpc: fix queryroutes bug that assumed precise number of returned routes

In this commit, we fix a bug in the query routes RPC that could at
times lead to a panic. This would happen if the number of returned
routes was less than the number of expected routes. To remedy this,
we’ll return the minimum of the number of requested routes, and the
number of routes actually returned.
This commit is contained in:
Olaoluwa Osuntokun 2018-02-26 16:31:40 -08:00
parent 158c78da60
commit 7f04d927a0
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
1 changed files with 9 additions and 1 deletions

View File

@ -2617,12 +2617,20 @@ func (r *rpcServer) QueryRoutes(ctx context.Context,
return nil, err
}
// As the number of returned routes can be less than the number of
// requested routes, we'll clamp down the length of the response to the
// minimum of the two.
numRoutes := int32(len(routes))
if in.NumRoutes < numRoutes {
numRoutes = in.NumRoutes
}
// For each valid route, we'll convert the result into the format
// required by the RPC system.
routeResp := &lnrpc.QueryRoutesResponse{
Routes: make([]*lnrpc.Route, 0, in.NumRoutes),
}
for i := int32(0); i < in.NumRoutes; i++ {
for i := int32(0); i < numRoutes; i++ {
routeResp.Routes = append(
routeResp.Routes, marshallRoute(routes[i]),
)