Skip to content

Commit

Permalink
Remove general shadowed variables.
Browse files Browse the repository at this point in the history
We shadow local variables in several places: generally, these changes
make the code clearer.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and niftynei committed Aug 31, 2022
1 parent 6a7d40f commit 6fe5708
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 99 deletions.
19 changes: 7 additions & 12 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,6 @@ static void send_commit(struct peer *peer)
* don't stress things by having more than one feerate change
* in-flight! */
if (feerate_changes_done(peer->channel->fee_states, false)) {
u8 *msg;

/* BOLT-919 #2:
*
* A sending node:
Expand Down Expand Up @@ -1309,8 +1307,6 @@ static void send_commit(struct peer *peer)
if (want_blockheight_update(peer, &our_blockheight)) {
if (blockheight_changes_done(peer->channel->blockheight_states,
false)) {
u8 *msg;

channel_update_blockheight(peer->channel,
our_blockheight);

Expand Down Expand Up @@ -2442,14 +2438,14 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
} else
tlvs = NULL;
#endif
u8 *msg = towire_update_add_htlc(NULL, &peer->channel_id,
h->id, h->amount,
&h->rhash,
abs_locktime_to_blocks(
&h->expiry),
h->routing
msg = towire_update_add_htlc(NULL, &peer->channel_id,
h->id, h->amount,
&h->rhash,
abs_locktime_to_blocks(
&h->expiry),
h->routing
#if EXPERIMENTAL_FEATURES
, tlvs
, tlvs
#endif
);
peer_write(peer->pps, take(msg));
Expand Down Expand Up @@ -2952,7 +2948,6 @@ static void peer_reconnect(struct peer *peer,
if (peer->funding_locked[LOCAL]
&& peer->next_index[LOCAL] == 1
&& next_commitment_number == 1) {
u8 *msg;
struct tlv_funding_locked_tlvs *tlvs = tlv_funding_locked_tlvs_new(tmpctx);

status_debug("Retransmitting funding_locked for channel %s",
Expand Down
8 changes: 4 additions & 4 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_sat base_fee;
struct amount_msat total_pay;
struct bitcoin_tx *tx;
size_t i, n, untrimmed;
size_t n, untrimmed;
/* Is this the lessor ? */
enum side lessor = !opener;
u32 *cltvs;
Expand Down Expand Up @@ -181,7 +181,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
{
struct amount_sat out = AMOUNT_SAT(0);
bool ok = true;
for (i = 0; i < tal_count(htlcs); i++) {
for (size_t i = 0; i < tal_count(htlcs); i++) {
if (!trim(htlcs[i], feerate_per_kw, dust_limit,
option_anchor_outputs, side))
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(htlcs[i]->amount));
Expand Down Expand Up @@ -215,7 +215,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* 4. For every offered HTLC, if it is not trimmed, add an
* [offered HTLC output](#offered-htlc-outputs).
*/
for (i = 0; i < tal_count(htlcs); i++) {
for (size_t i = 0; i < tal_count(htlcs); i++) {
if (htlc_owner(htlcs[i]) != side)
continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit,
Expand All @@ -233,7 +233,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* 5. For every received HTLC, if it is not trimmed, add an
* [received HTLC output](#received-htlc-outputs).
*/
for (i = 0; i < tal_count(htlcs); i++) {
for (size_t i = 0; i < tal_count(htlcs); i++) {
if (htlc_owner(htlcs[i]) == side)
continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit,
Expand Down
6 changes: 3 additions & 3 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ int main(int argc, char *argv[])
{
setup_locale();

int fd, i;
int fd;
size_t off;
const char *method;
char *cmd, *resp, *idstr;
Expand Down Expand Up @@ -715,7 +715,7 @@ int main(int argc, char *argv[])

if (input == KEYWORDS) {
tal_append_fmt(&cmd, "{ ");
for (i = 2; i < argc; i++) {
for (size_t i = 2; i < argc; i++) {
const char *eq = strchr(argv[i], '=');

if (!eq)
Expand All @@ -730,7 +730,7 @@ int main(int argc, char *argv[])
tal_append_fmt(&cmd, "} }");
} else {
tal_append_fmt(&cmd, "[ ");
for (i = 2; i < argc; i++)
for (size_t i = 2; i < argc; i++)
add_input(&cmd, argv[i], i, argc);
tal_append_fmt(&cmd, "] }");
}
Expand Down
2 changes: 1 addition & 1 deletion common/test/run-bolt12_period.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ int main(int argc, char *argv[])
/* Replace day */
tparts[0] = "UTC";
/* Now each part should appear. */
for (size_t i = 0; tparts[i]; i++)
for (i = 0; tparts[i]; i++)
assert(strstr(timestring, tparts[i]));
assert(offer_period_start(base, n, &recurrence) == secs);
}
Expand Down
12 changes: 6 additions & 6 deletions common/test/run-route_blinding_override_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ int main(int argc, char *argv[])
json_for_each_arr(i, t, unblinding_hops) {
struct privkey me;
struct secret ss;
struct pubkey blinding, expected_blinding;
struct pubkey blindingpub, expected_blinding;
struct pubkey onion_key, next_node;

assert(json_to_secret(json,
Expand All @@ -337,20 +337,20 @@ int main(int argc, char *argv[])
mykey = &me;
assert(json_to_pubkey(json,
json_get_member(json, t, "ephemeral_pubkey"),
&blinding));
&blindingpub));

assert(unblind_onion(&blinding, test_ecdh, &onion_key, &ss));
assert(unblind_onion(&blindingpub, test_ecdh, &onion_key, &ss));
if (i != unblinding_hops->size - 1) {
assert(decrypt_enctlv(&blinding, &ss, encrypted_data[i], &next_node, &blinding));
assert(decrypt_enctlv(&blindingpub, &ss, encrypted_data[i], &next_node, &blindingpub));
assert(json_to_pubkey(json,
json_get_member(json, t, "next_ephemeral_pubkey"),
&expected_blinding));
assert(pubkey_eq(&blinding, &expected_blinding));
assert(pubkey_eq(&blindingpub, &expected_blinding));
} else {
struct secret *path_id;
struct pubkey my_id, alias;
assert(pubkey_from_privkey(&me, &my_id));
assert(decrypt_final_enctlv(tmpctx, &blinding, &ss,
assert(decrypt_final_enctlv(tmpctx, &blindingpub, &ss,
encrypted_data[i],
&my_id, &alias,
&path_id));
Expand Down
6 changes: 3 additions & 3 deletions devtools/bolt11-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ int main(int argc, char *argv[])
const char *method;
struct bolt11 *b11;
struct bolt11_field *extra;
size_t i;
char *fail, *description = NULL;

common_setup(argv[0]);
Expand Down Expand Up @@ -120,7 +119,7 @@ int main(int argc, char *argv[])
}
printf("\n");
}
for (i = 0; i < tal_count(b11->fallbacks); i++) {
for (size_t i = 0; i < tal_count(b11->fallbacks); i++) {
struct bitcoin_address pkh;
struct ripemd160 sh;
struct sha256 wsh;
Expand Down Expand Up @@ -148,7 +147,7 @@ int main(int argc, char *argv[])
}
}

for (i = 0; i < tal_count(b11->routes); i++) {
for (size_t i = 0; i < tal_count(b11->routes); i++) {
printf("route: (node/chanid/fee/expirydelta) ");
for (size_t n = 0; n < tal_count(b11->routes[i]); n++) {
printf(" %s/%s/%u/%u/%u",
Expand All @@ -169,6 +168,7 @@ int main(int argc, char *argv[])

list_for_each(&b11->extra_fields, extra, list) {
char *data = tal_arr(ctx, char, tal_count(extra->data)+1);
size_t i;

for (i = 0; i < tal_count(extra->data); i++)
data[i] = bech32_charset[extra->data[i]];
Expand Down
4 changes: 2 additions & 2 deletions devtools/decodemsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ int main(int argc, char *argv[])
}
m = tal_dup_arr(f, u8, f + off, be16_to_cpu(len), 0);
if (printtlv) {
size_t len = tal_bytelen(m);
ok &= printtlv("", &m, &len);
size_t mlen = tal_bytelen(m);
ok &= printtlv("", &m, &mlen);
} else {
ok &= printwire(m);
}
Expand Down
2 changes: 1 addition & 1 deletion devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ static struct io_plan *handshake_success(struct io_conn *conn,
struct oneshot *timer,
char **args)
{
u8 *msg;
int peer_fd = io_conn_fd(conn);
struct pollfd pollfd[2];

Expand All @@ -179,6 +178,7 @@ static struct io_plan *handshake_success(struct io_conn *conn,
OPTIONAL_FEATURE(OPT_INITIAL_ROUTING_SYNC));

if (!no_init) {
u8 *msg;
struct tlv_init_tlvs *tlvs = NULL;
if (explicit_network) {
tlvs = tlv_init_tlvs_new(NULL);
Expand Down
3 changes: 1 addition & 2 deletions devtools/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ int main(int argc, char *argv[])
for (n = gossmap_first_node(map);
n;
n = gossmap_next_node(map, n)) {
struct node_id srcid, dstid;
struct node_id srcid;

gossmap_node_get_id(map, n, &srcid);
gossmap_node_get_id(map, dst, &dstid);
printf("# %s->%s\n",
type_to_string(tmpctx, struct node_id, &srcid),
type_to_string(tmpctx, struct node_id, &dstid));
Expand Down
6 changes: 3 additions & 3 deletions gossipd/gossip_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,17 +785,17 @@ u32 gossip_store_load(struct routing_state *rstate, struct gossip_store *gs)

switch (fromwire_peektype(msg)) {
case WIRE_GOSSIP_STORE_PRIVATE_CHANNEL: {
u8 *chan_ann;
u8 *priv_chan_ann;
struct amount_sat sat;
if (!fromwire_gossip_store_private_channel(msg, msg,
&sat,
&chan_ann)) {
&priv_chan_ann)) {
bad = "Bad private_channel";
goto badmsg;
}

if (!routing_add_private_channel(rstate, NULL,
sat, chan_ann,
sat, priv_chan_ann,
gs->len)) {
bad = "Bad add_private_channel";
goto badmsg;
Expand Down
6 changes: 3 additions & 3 deletions gossipd/queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,9 @@ bool query_channel_range(struct daemon *daemon,
struct peer *peer,
u32 first_blocknum, u32 number_of_blocks,
enum query_option_flags qflags,
void (*cb)(struct peer *peer,
u32 first_blocknum, u32 number_of_blocks,
const struct range_query_reply *replies))
void (*cb)(struct peer *,
u32, u32,
const struct range_query_reply *))
{
u8 *msg;
struct tlv_query_channel_range_tlvs *tlvs;
Expand Down
9 changes: 5 additions & 4 deletions gossipd/queries.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ bool query_channel_range(struct daemon *daemon,
struct peer *peer,
u32 first_blocknum, u32 number_of_blocks,
enum query_option_flags qflags,
void (*cb)(struct peer *peer,
u32 first_blocknum, u32 number_of_blocks,
const struct range_query_reply *replies));
void (*cb)(struct peer *peer_,
u32 first_blocknum_,
u32 number_of_blocks_,
const struct range_query_reply *replies_));

/* Ask this peer for info about an array of scids, with optional query_flags */
bool query_short_channel_ids(struct daemon *daemon,
struct peer *peer,
const struct short_channel_id *scids,
const u8 *query_flags,
void (*cb)(struct peer *peer, bool complete));
void (*cb)(struct peer *peer_, bool complete));

#if DEVELOPER
struct io_plan *query_scids_req(struct io_conn *conn,
Expand Down
9 changes: 5 additions & 4 deletions gossipd/test/run-next_block_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ bool query_channel_range(struct daemon *daemon UNNEEDED,
struct peer *peer UNNEEDED,
u32 first_blocknum UNNEEDED, u32 number_of_blocks UNNEEDED,
enum query_option_flags qflags UNNEEDED,
void (*cb)(struct peer *peer UNNEEDED,
u32 first_blocknum UNNEEDED, u32 number_of_blocks UNNEEDED,
const struct range_query_reply *replies))
void (*cb)(struct peer *peer_ UNNEEDED,
u32 first_blocknum_ UNNEEDED,
u32 number_of_blocks_ UNNEEDED,
const struct range_query_reply *replies_))
{ fprintf(stderr, "query_channel_range called!\n"); abort(); }
/* Generated stub for query_short_channel_ids */
bool query_short_channel_ids(struct daemon *daemon UNNEEDED,
struct peer *peer UNNEEDED,
const struct short_channel_id *scids UNNEEDED,
const u8 *query_flags UNNEEDED,
void (*cb)(struct peer *peer UNNEEDED, bool complete))
void (*cb)(struct peer *peer_ UNNEEDED, bool complete))
{ fprintf(stderr, "query_short_channel_ids called!\n"); abort(); }
/* Generated stub for queue_peer_msg */
void queue_peer_msg(struct peer *peer UNNEEDED, const u8 *msg TAKES UNNEEDED)
Expand Down
6 changes: 3 additions & 3 deletions lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,9 @@ static void getutxout_callback(const char *buf, const jsmntok_t *toks,

void bitcoind_getutxout_(struct bitcoind *bitcoind,
const struct bitcoin_outpoint *outpoint,
void (*cb)(struct bitcoind *bitcoind,
const struct bitcoin_tx_output *txout,
void *arg),
void (*cb)(struct bitcoind *,
const struct bitcoin_tx_output *,
void *),
void *cb_arg)
{
struct jsonrpc_request *req;
Expand Down
6 changes: 3 additions & 3 deletions lightningd/bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ void bitcoind_getrawblockbyheight_(struct bitcoind *bitcoind,

void bitcoind_getutxout_(struct bitcoind *bitcoind,
const struct bitcoin_outpoint *outpoint,
void (*cb)(struct bitcoind *bitcoind,
const struct bitcoin_tx_output *txout,
void *arg),
void (*cb)(struct bitcoind *,
const struct bitcoin_tx_output *,
void *),
void *arg);
#define bitcoind_getutxout(bitcoind_, outpoint_, cb, arg) \
bitcoind_getutxout_((bitcoind_), (outpoint_), \
Expand Down
6 changes: 3 additions & 3 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ static void remove_tip(struct chain_topology *topo)
{
struct block *b = topo->tip;
struct bitcoin_txid *txs;
size_t i, n;
size_t n;
const struct short_channel_id *removed_scids;

log_debug(topo->log, "Removing stale block %u: %s",
Expand All @@ -780,7 +780,7 @@ static void remove_tip(struct chain_topology *topo)
n = tal_count(txs);

/* Notify that txs are kicked out (their height will be set NULL in db) */
for (i = 0; i < n; i++)
for (size_t i = 0; i < n; i++)
txwatch_fire(topo, &txs[i], 0);

/* Grab these before we delete block from db */
Expand All @@ -795,7 +795,7 @@ static void remove_tip(struct chain_topology *topo)

/* These no longer exist, so gossipd drops any reference to them just
* as if they were spent. */
for (size_t i=0; i<tal_count(removed_scids); i++)
for (size_t i = 0; i < tal_count(removed_scids); i++)
gossipd_notify_spend(topo->bitcoind->ld, &removed_scids[i]);
}

Expand Down
15 changes: 8 additions & 7 deletions lightningd/memdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ static void json_add_ptr(struct json_stream *response, const char *name,
}

static size_t add_memdump(struct json_stream *response,
const char *name, const tal_t *root,
struct command *cmd)
const char *fieldname, const tal_t *root,
struct command *cmd)
{
const tal_t *i;
size_t cumulative_size = 0;

json_array_start(response, name);
for (i = tal_first(root); i; i = tal_next(i)) {
json_array_start(response, fieldname);
for (const tal_t *i = tal_first(root); i; i = tal_next(i)) {
const char *name = tal_name(i);
size_t size = tal_bytelen(i);

Expand Down Expand Up @@ -176,9 +175,11 @@ static void finish_report(const struct leak_detect *leaks)
json_object_end(response);
}

for (size_t i = 0; i < tal_count(leaks->leakers); i++) {
for (size_t num_leakers = 0;
num_leakers < tal_count(leaks->leakers);
num_leakers++) {
json_object_start(response, NULL);
json_add_string(response, "subdaemon", leaks->leakers[i]);
json_add_string(response, "subdaemon", leaks->leakers[num_leakers]);
json_object_end(response);
}
json_array_end(response);
Expand Down
Loading

0 comments on commit 6fe5708

Please sign in to comment.