From bb28bbd4708d6e64c5d289dabe2a3de9444893b7 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Aug 2016 14:23:46 +0930 Subject: [PATCH] peer: always initialize commit_info commit number, other fields. We used to use talz, but that prevents valgrind from noticing when we use uninitialized fields. Signed-off-by: Rusty Russell --- daemon/peer.c | 15 +++++++++------ daemon/peer.h | 2 +- state.c | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index 16680eac6..4c292cf21 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -733,7 +733,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt) { Pkt *err; struct sha256 preimage; - struct commit_info *ci = new_commit_info(peer); + struct commit_info *ci; /* FIXME: We can actually merge these two... */ static const struct state_table commit_changes[] = { { RCVD_ADD_REVOCATION, RCVD_ADD_ACK_COMMIT }, @@ -748,6 +748,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt) { RCVD_REMOVE_ACK_COMMIT, SENT_REMOVE_ACK_REVOCATION } }; + ci = new_commit_info(peer, peer->local.commit->commit_num + 1); ci->sig = tal(ci, struct bitcoin_signature); err = accept_pkt_commit(peer, pkt, ci->sig); if (err) @@ -762,7 +763,6 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt) return pkt_err(peer, "Empty commit"); /* Create new commit info for this commit tx. */ - ci->commit_num = peer->local.commit->commit_num + 1; ci->revocation_hash = peer->local.next_revocation_hash; /* BOLT #2: @@ -1495,7 +1495,7 @@ static void do_commit(struct peer *peer, struct command *jsoncmd) assert(!peer->commit_jsoncmd); peer->commit_jsoncmd = jsoncmd; - ci = new_commit_info(peer); + ci = new_commit_info(peer, peer->remote.commit->commit_num + 1); assert(!peer->their_prev_revocation_hash); peer->their_prev_revocation_hash @@ -1511,7 +1511,6 @@ static void do_commit(struct peer *peer, struct command *jsoncmd) fatal("sent commit with no changes"); /* Create new commit info for this commit tx. */ - ci->commit_num = peer->remote.commit->commit_num + 1; ci->revocation_hash = peer->remote.next_revocation_hash; /* BOLT #2: * @@ -1566,9 +1565,13 @@ static void try_commit(struct peer *peer) } } -struct commit_info *new_commit_info(const tal_t *ctx) +struct commit_info *new_commit_info(const tal_t *ctx, u64 commit_num) { - struct commit_info *ci = talz(ctx, struct commit_info); + struct commit_info *ci = tal(ctx, struct commit_info); + ci->commit_num = commit_num; + ci->tx = NULL; + ci->cstate = NULL; + ci->sig = NULL; return ci; } diff --git a/daemon/peer.h b/daemon/peer.h index a35533520..d6a1ab1b0 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -222,7 +222,7 @@ void peer_add_their_commit(struct peer *peer, const struct sha256_double *txid, u64 commit_num); /* Allocate a new commit_info struct. */ -struct commit_info *new_commit_info(const tal_t *ctx); +struct commit_info *new_commit_info(const tal_t *ctx, u64 commit_num); /* Freeing removes from map, too */ struct htlc *peer_new_htlc(struct peer *peer, diff --git a/state.c b/state.c index 9efeb6ac2..d3d60980d 100644 --- a/state.c +++ b/state.c @@ -28,7 +28,7 @@ static void send_open_pkt(struct peer *peer, { /* Set up out commit info now: rest gets done in setup_first_commit * once anchor is established. */ - peer->local.commit = new_commit_info(peer); + peer->local.commit = new_commit_info(peer, 0); peer->local.commit->revocation_hash = peer->local.next_revocation_hash; peer_get_revocation_hash(peer, 1, &peer->local.next_revocation_hash); @@ -37,7 +37,7 @@ static void send_open_pkt(struct peer *peer, static Pkt *init_from_pkt_open(struct peer *peer, const Pkt *pkt) { - struct commit_info *ci = new_commit_info(peer); + struct commit_info *ci = new_commit_info(peer, 0); Pkt *err; err = accept_pkt_open(peer, pkt, &ci->revocation_hash,