From 7e9750ffee4059b35d5b29321150173023fe0055 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Sun, 25 Mar 2018 21:51:11 +0200 Subject: [PATCH] Reduce variable scopes --- channeld/channel.c | 5 ++--- common/bolt11.c | 4 ++-- common/test/run-json.c | 3 +-- gossipd/routing.c | 3 +-- hsmd/hsm.c | 6 ++---- lightningd/bitcoind.c | 7 +++---- lightningd/channel.c | 3 +-- lightningd/connect_control.c | 3 +-- lightningd/jsonrpc.c | 7 +++---- lightningd/pay.c | 9 +++------ lightningd/payalgo.c | 5 ++--- wallet/db.c | 3 +-- wallet/txfilter.c | 3 +-- wallet/wallet.c | 6 ++---- wallet/walletrpc.c | 28 ++++++++++------------------ 15 files changed, 35 insertions(+), 60 deletions(-) diff --git a/channeld/channel.c b/channeld/channel.c index 393893f30..37c481f4e 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -2299,11 +2299,10 @@ static void handle_fail(struct peer *peer, const u8 *inmsg) failcode); } else { u8 *reply; - u8 *failmsg; if (failcode) { - failmsg = make_failmsg(inmsg, peer, h, - failcode, &scid); + u8 *failmsg = make_failmsg(inmsg, peer, h, + failcode, &scid); errpkt = create_onionreply(inmsg, h->shared_secret, failmsg); diff --git a/common/bolt11.c b/common/bolt11.c index bd372468c..c453ee61b 100644 --- a/common/bolt11.c +++ b/common/bolt11.c @@ -889,14 +889,12 @@ char *bolt11_encode_(const tal_t *ctx, { u5 *data = tal_arr(tmpctx, u5, 0); char *hrp, *output; - char postfix; u64 amount; struct bolt11_field *extra; secp256k1_ecdsa_recoverable_signature rsig; u8 sig_and_recid[65]; u8 *hrpu8; int recid; - size_t i; /* BOLT #11: * @@ -905,10 +903,12 @@ char *bolt11_encode_(const tal_t *ctx, * representation possible. */ if (b11->msatoshi) { + char postfix; if (*b11->msatoshi % MSAT_PER_BTC == 0) { postfix = '\0'; amount = *b11->msatoshi / MSAT_PER_BTC; } else { + size_t i; for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) { if (!(*b11->msatoshi * 10 % multipliers[i].m10)) break; diff --git a/common/test/run-json.c b/common/test/run-json.c index d63658f1d..6ebc811ac 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -82,7 +82,6 @@ static int test_json_filter(void) static void test_json_escape(void) { int i; - const char *str; for (i = 1; i < 256; i++) { char badstr[2]; @@ -97,7 +96,7 @@ static void test_json_escape(void) json_add_escaped_string(result, "x", take(esc)); json_object_end(result); - str = json_result_string(result); + const char *str = json_result_string(result); if (i == '\\' || i == '"' || i == '\n' || i == '\r' || i == '\b' || i == '\t' || i == '\f') diff --git a/gossipd/routing.c b/gossipd/routing.c index 9932a9187..004edcaa6 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -1302,7 +1302,6 @@ void routing_failure(struct routing_state *rstate, const u8 *channel_update) { struct node *node; - int i; time_t now = time_now().ts.tv_sec; status_trace("Received routing failure 0x%04x (%s), " @@ -1330,7 +1329,7 @@ void routing_failure(struct routing_state *rstate, * */ if (failcode & NODE) { - for (i = 0; i < tal_count(node->chans); ++i) { + for (int i = 0; i < tal_count(node->chans); ++i) { routing_failure_channel_out(tmpctx, node, failcode, node->chans[i], now); diff --git a/hsmd/hsm.c b/hsmd/hsm.c index 9d2d0b0f1..23616cacb 100644 --- a/hsmd/hsm.c +++ b/hsmd/hsm.c @@ -608,7 +608,6 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg) struct pubkey local_pubkey, remote_pubkey; struct utxo **utxomap; struct bitcoin_tx *tx; - u8 *wscript; u16 outnum; size_t i; struct pubkey changekey; @@ -644,7 +643,7 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg) subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey); else subscript = NULL; - wscript = p2wpkh_scriptcode(tmpctx, &inkey); + u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey); sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey, &sig); @@ -673,7 +672,6 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg) u64 satoshi_out, change_out; u32 change_keyindex; struct utxo **utxos; - u8 *wscript; u8 **scriptSigs; struct bitcoin_tx *tx; struct ext_key ext; @@ -715,7 +713,7 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg) subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey); else subscript = NULL; - wscript = p2wpkh_scriptcode(tmpctx, &inkey); + u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey); sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey, &sig); diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index 611c2c212..73877dca3 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -738,10 +738,9 @@ static void fatal_bitcoind_failure(struct bitcoind *bitcoind, const char *error_ void wait_for_bitcoind(struct bitcoind *bitcoind) { - int from, ret, status; + int from, status; pid_t child; const char **cmd = cmdarr(bitcoind, bitcoind, "echo", NULL); - char *output; bool printed = false; for (;;) { @@ -753,12 +752,12 @@ void wait_for_bitcoind(struct bitcoind *bitcoind) fatal("%s exec failed: %s", cmd[0], strerror(errno)); } - output = grab_fd(cmd, from); + char *output = grab_fd(cmd, from); if (!output) fatal("Reading from %s failed: %s", cmd[0], strerror(errno)); - ret = waitpid(child, &status, 0); + int ret = waitpid(child, &status, 0); if (ret != child) fatal("Waiting for %s: %s", cmd[0], strerror(errno)); if (!WIFEXITED(status)) diff --git a/lightningd/channel.c b/lightningd/channel.c index 18a1cb8a7..605e1aa50 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -287,7 +287,6 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...) struct lightningd *ld = channel->peer->ld; va_list ap; char *why; - u8 *msg; struct channel_id cid; va_start(ap, fmt); @@ -295,7 +294,7 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...) va_end(ap); if (channel->scid) { - msg = towire_gossip_disable_channel(NULL, + u8 *msg = towire_gossip_disable_channel(NULL, channel->scid, channel->peer->direction, false); diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index f4934f157..ef213d0fe 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -111,7 +111,6 @@ static void json_connect(struct command *cmd, char *id_str; char *atptr; char *ataddr = NULL; - int atidx; const char *name; struct wireaddr addr; u8 *msg; @@ -130,7 +129,7 @@ static void json_connect(struct command *cmd, idtok->end - idtok->start); atptr = strchr(id_str, '@'); if (atptr) { - atidx = atptr - id_str; + int atidx = atptr - id_str; ataddr = tal_strdup(cmd, atptr + 1); /* Cut id. */ idtok->end = idtok->start + atidx; diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 51ebdda5f..1ffc00160 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -487,7 +487,7 @@ bool json_get_params(struct command *cmd, const char **names; size_t num_names; /* Uninitialized warnings on p and end */ - const jsmntok_t **tokptr, *p = NULL, *end = NULL; + const jsmntok_t *p = NULL, *end = NULL; if (param->type == JSMN_ARRAY) { if (param->size == 0) @@ -505,7 +505,7 @@ bool json_get_params(struct command *cmd, names = tal_arr(cmd, const char *, num_names + 1); va_start(ap, param); while ((names[num_names] = va_arg(ap, const char *)) != NULL) { - tokptr = va_arg(ap, const jsmntok_t **); + const jsmntok_t **tokptr = va_arg(ap, const jsmntok_t **); bool compulsory = true; if (names[num_names][0] == '?') { names[num_names]++; @@ -788,7 +788,6 @@ json_tok_address_scriptpubkey(const tal_t *cxt, * of fixed size 40 will not overflow. */ uint8_t witness_program[40]; size_t witness_program_len; - bool witness_ok; char *addrz; const char *bip173; @@ -826,7 +825,7 @@ json_tok_address_scriptpubkey(const tal_t *cxt, &witness_program_len, addrz); if (bip173) { - witness_ok = false; + bool witness_ok = false; if (witness_version == 0 && (witness_program_len == 20 || witness_program_len == 32)) { witness_ok = true; diff --git a/lightningd/pay.c b/lightningd/pay.c index fcb92b2c7..1441f1066 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -419,8 +419,6 @@ void payment_store(struct lightningd *ld, void payment_failed(struct lightningd *ld, const struct htlc_out *hout, const char *localfail) { - struct onionreply *reply; - struct secret *path_secrets; struct wallet_payment *payment; struct routing_failure* fail = NULL; const char *failmsg; @@ -468,8 +466,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout, assert(!hout->failcode); failmsg = "reply from remote"; /* Try to parse reply. */ - path_secrets = payment->path_secrets; - reply = unwrap_onionreply(tmpctx, path_secrets, + struct secret *path_secrets = payment->path_secrets; + struct onionreply *reply = unwrap_onionreply(tmpctx, path_secrets, tal_count(path_secrets), hout->failuremsg); if (!reply) { @@ -895,11 +893,10 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r, void *vcmd) { struct command *cmd = (struct command*) vcmd; - struct json_result *response; if (!r->succeeded && r->errorcode == PAY_IN_PROGRESS) { /* This is normal for sendpay. Succeed. */ - response = new_json_result(cmd); + struct json_result *response = new_json_result(cmd); json_object_start(response, NULL); json_add_string(response, "message", "Monitor status with listpayments or waitsendpay"); diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index b157b0a98..ca34015f2 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -200,7 +200,7 @@ json_pay_success(struct pay *pay, static void json_pay_failure(struct pay *pay, const struct sendpay_result *r) { - struct json_result *data = NULL; + struct json_result *data; const char *msg = NULL; struct routing_failure *fail; @@ -485,12 +485,11 @@ static bool json_pay_try(struct pay *pay) u8 *req; struct command *cmd = pay->cmd; struct timeabs now = time_now(); - struct json_result *data; struct siphash_seed seed; /* If too late anyway, fail now. */ if (time_after(now, pay->expiry)) { - data = new_json_result(cmd); + struct json_result *data = new_json_result(cmd); json_object_start(data, NULL); json_add_num(data, "now", now.ts.tv_sec); json_add_num(data, "expiry", pay->expiry.ts.tv_sec); diff --git a/wallet/db.c b/wallet/db.c index 3e16ee75c..f28fb8ea2 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -525,7 +525,6 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval) { int err; s64 res = defval; - const unsigned char *stringvar; sqlite3_stmt *stmt = db_query(__func__, db, "SELECT val FROM vars WHERE name='%s' LIMIT 1", varname); @@ -535,7 +534,7 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval) err = sqlite3_step(stmt); if (err == SQLITE_ROW) { - stringvar = sqlite3_column_text(stmt, 0); + const unsigned char *stringvar = sqlite3_column_text(stmt, 0); res = atol((const char *)stringvar); } sqlite3_finalize(stmt); diff --git a/wallet/txfilter.c b/wallet/txfilter.c index c0231be80..10e6ea7dc 100644 --- a/wallet/txfilter.c +++ b/wallet/txfilter.c @@ -75,9 +75,8 @@ void txfilter_add_derkey(struct txfilter *filter, bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx) { - u8 *oscript; for (size_t i = 0; i < tal_count(tx->output); i++) { - oscript = tx->output[i].script; + u8 *oscript = tx->output[i].script; for (size_t j = 0; j < tal_count(filter->scriptpubkeys); j++) { if (scripteq(oscript, filter->scriptpubkeys[j])) diff --git a/wallet/wallet.c b/wallet/wallet.c index 549afbbc4..5e4d4b5a9 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -526,7 +526,6 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, struct peer *peer) { bool ok; - const unsigned char *addrstr; sqlite3_stmt *stmt = db_prepare(w->db, "SELECT id, node_id, address FROM peers WHERE node_id=?;"); sqlite3_bind_pubkey(stmt, 1, nodeid); @@ -534,7 +533,7 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, if (ok) { peer->dbid = sqlite3_column_int64(stmt, 0); ok &= sqlite3_column_pubkey(stmt, 1, &peer->id); - addrstr = sqlite3_column_text(stmt, 2); + const unsigned char *addrstr = sqlite3_column_text(stmt, 2); if (addrstr) parse_wireaddr((const char*)addrstr, &peer->addr, DEFAULT_PORT, NULL); @@ -1852,7 +1851,6 @@ void wallet_payment_set_failinfo(struct wallet *wallet, const u8 *failupdate /*tal_arr*/) { sqlite3_stmt *stmt; - struct short_channel_id *scid; stmt = db_prepare(wallet->db, "UPDATE payments" @@ -1880,7 +1878,7 @@ void wallet_payment_set_failinfo(struct wallet *wallet, if (failchannel) { /* sqlite3_bind_short_channel_id requires the input * channel to be tal-allocated... */ - scid = tal(tmpctx, struct short_channel_id); + struct short_channel_id *scid = tal(tmpctx, struct short_channel_id); *scid = *failchannel; sqlite3_bind_short_channel_id(stmt, 6, scid); } else diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 40d6f6b5f..067fe0efb 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -46,7 +46,6 @@ static void wallet_withdrawal_broadcast(struct bitcoind *bitcoind UNUSED, { struct command *cmd = withdraw->cmd; struct lightningd *ld = withdraw->cmd->ld; - struct bitcoin_tx *tx; u64 change_satoshi = 0; /* Massage output into shape so it doesn't kill the JSON serialization */ @@ -57,7 +56,7 @@ static void wallet_withdrawal_broadcast(struct bitcoind *bitcoind UNUSED, /* Parse the tx and extract the change output. We * generated the hex tx, so this should always work */ - tx = bitcoin_tx_from_hex(withdraw, withdraw->hextx, strlen(withdraw->hextx)); + struct bitcoin_tx *tx = bitcoin_tx_from_hex(withdraw, withdraw->hextx, strlen(withdraw->hextx)); assert(tx != NULL); wallet_extract_owned_outputs(ld->wallet, tx, NULL, &change_satoshi); @@ -208,11 +207,9 @@ static void json_newaddr(struct command *cmd, const char *buffer UNUSED, struct ripemd160 h160; struct pubkey pubkey; jsmntok_t *addrtype; - u8 *redeemscript; - bool is_p2wpkh, ok; + bool is_p2wpkh; s64 keyidx; char *out; - const char *hrp; if (!json_get_params(cmd, buffer, params, "?addresstype", &addrtype, NULL)) { @@ -251,18 +248,18 @@ static void json_newaddr(struct command *cmd, const char *buffer UNUSED, txfilter_add_derkey(cmd->ld->owned_txfilter, ext.pub_key); if (is_p2wpkh) { - hrp = get_chainparams(cmd->ld)->bip173_name; + const char *hrp = get_chainparams(cmd->ld)->bip173_name; /* out buffer is 73 + strlen(human readable part). see bech32.h */ out = tal_arr(cmd, char, 73 + strlen(hrp)); pubkey_to_hash160(&pubkey, &h160); - ok = segwit_addr_encode(out, hrp, 0, h160.u.u8, sizeof(h160.u.u8)); + bool ok = segwit_addr_encode(out, hrp, 0, h160.u.u8, sizeof(h160.u.u8)); if (!ok) { command_fail(cmd, "p2wpkh address encoding failure."); return; } } else { - redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); + u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); sha256(&h, redeemscript, tal_count(redeemscript)); ripemd160(&h160, h.u.u8, sizeof(h)); out = p2sh_to_base58(cmd, @@ -292,11 +289,6 @@ static void json_listaddrs(struct command *cmd, struct ripemd160 h160; struct pubkey pubkey; jsmntok_t *bip32tok; - u8 *redeemscript; - bool ok; - char *out_p2sh; - char *out_p2wpkh; - const char *hrp; u64 bip32_max_index; if (!json_get_params(cmd, buffer, params, @@ -330,18 +322,18 @@ static void json_listaddrs(struct command *cmd, } // p2sh - redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); + u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); sha256(&h, redeemscript, tal_count(redeemscript)); ripemd160(&h160, h.u.u8, sizeof(h)); - out_p2sh = p2sh_to_base58(cmd, + char *out_p2sh = p2sh_to_base58(cmd, get_chainparams(cmd->ld)->testnet, &h160); // bech32 : p2wpkh - hrp = get_chainparams(cmd->ld)->bip173_name; + const char *hrp = get_chainparams(cmd->ld)->bip173_name; /* out buffer is 73 + strlen(human readable part). see bech32.h */ - out_p2wpkh = tal_arr(cmd, char, 73 + strlen(hrp)); + char *out_p2wpkh = tal_arr(cmd, char, 73 + strlen(hrp)); pubkey_to_hash160(&pubkey, &h160); - ok = segwit_addr_encode(out_p2wpkh, hrp, 0, h160.u.u8, sizeof(h160.u.u8)); + bool ok = segwit_addr_encode(out_p2wpkh, hrp, 0, h160.u.u8, sizeof(h160.u.u8)); if (!ok) { command_fail(cmd, "p2wpkh address encoding failure."); return;