From 7f04d927a04ed5cb6843f2f47e2b50989d251f91 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 26 Feb 2018 16:31:40 -0800 Subject: [PATCH] rpc: fix queryroutes bug that assumed precise number of returned routes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- rpcserver.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/rpcserver.go b/rpcserver.go index 673a5464..0991921b 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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]), )