json_getroute: don't leak.

Allocate the route off the current command, not dstate.  And in the
case where the route is somehow not via a peer, don't leak memory.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-11-04 11:17:04 +10:30
parent a902193874
commit b40c4ae766
3 changed files with 15 additions and 12 deletions

View file

@ -241,7 +241,8 @@ static void json_getroute(struct command *cmd,
return;
}
peer = find_route(cmd->dstate, &id, msatoshi, riskfactor, &fee, &route);
peer = find_route(cmd, cmd->dstate, &id, msatoshi, riskfactor,
&fee, &route);
if (!peer) {
command_fail(cmd, "no route found");
return;

View file

@ -273,7 +273,8 @@ static void bfg_one_edge(struct node *node, size_t edgenum, double riskfactor)
}
}
struct peer *find_route(struct lightningd_state *dstate,
struct peer *find_route(const tal_t *ctx,
struct lightningd_state *dstate,
const struct pubkey *to,
u64 msatoshi,
double riskfactor,
@ -350,15 +351,6 @@ struct peer *find_route(struct lightningd_state *dstate,
dst = dst->bfg[best].prev->dst;
best--;
*fee = dst->bfg[best].total - msatoshi;
*route = tal_arr(dstate, struct node_connection *, best);
for (i = 0, n = dst;
i < best;
n = n->bfg[best-i].prev->dst, i++) {
(*route)[i] = n->bfg[best-i].prev;
}
assert(n == src);
/* We should only add routes if we have a peer. */
first = find_peer(dstate, &dst->id);
if (!first) {
@ -367,6 +359,15 @@ struct peer *find_route(struct lightningd_state *dstate,
return NULL;
}
*fee = dst->bfg[best].total - msatoshi;
*route = tal_arr(ctx, struct node_connection *, best);
for (i = 0, n = dst;
i < best;
n = n->bfg[best-i].prev->dst, i++) {
(*route)[i] = n->bfg[best-i].prev;
}
assert(n == src);
msatoshi += *fee;
log_info(dstate->base_log, "find_route:");
log_add_struct(dstate->base_log, "via %s", struct pubkey, first->id);

View file

@ -67,7 +67,8 @@ struct node_connection *add_connection(struct lightningd_state *dstate,
void remove_connection(struct lightningd_state *dstate,
const struct pubkey *src, const struct pubkey *dst);
struct peer *find_route(struct lightningd_state *dstate,
struct peer *find_route(const tal_t *ctx,
struct lightningd_state *dstate,
const struct pubkey *to,
u64 msatoshi,
double riskfactor,