Skip to content

Commit 7e9750f

Browse files
practicalswiftrustyrussell
authored andcommitted
Reduce variable scopes
1 parent 94ca824 commit 7e9750f

File tree

15 files changed

+35
-60
lines changed

15 files changed

+35
-60
lines changed

channeld/channel.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,11 +2299,10 @@ static void handle_fail(struct peer *peer, const u8 *inmsg)
22992299
failcode);
23002300
} else {
23012301
u8 *reply;
2302-
u8 *failmsg;
23032302

23042303
if (failcode) {
2305-
failmsg = make_failmsg(inmsg, peer, h,
2306-
failcode, &scid);
2304+
u8 *failmsg = make_failmsg(inmsg, peer, h,
2305+
failcode, &scid);
23072306
errpkt = create_onionreply(inmsg,
23082307
h->shared_secret,
23092308
failmsg);

common/bolt11.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,14 +889,12 @@ char *bolt11_encode_(const tal_t *ctx,
889889
{
890890
u5 *data = tal_arr(tmpctx, u5, 0);
891891
char *hrp, *output;
892-
char postfix;
893892
u64 amount;
894893
struct bolt11_field *extra;
895894
secp256k1_ecdsa_recoverable_signature rsig;
896895
u8 sig_and_recid[65];
897896
u8 *hrpu8;
898897
int recid;
899-
size_t i;
900898

901899
/* BOLT #11:
902900
*
@@ -905,10 +903,12 @@ char *bolt11_encode_(const tal_t *ctx,
905903
* representation possible.
906904
*/
907905
if (b11->msatoshi) {
906+
char postfix;
908907
if (*b11->msatoshi % MSAT_PER_BTC == 0) {
909908
postfix = '\0';
910909
amount = *b11->msatoshi / MSAT_PER_BTC;
911910
} else {
911+
size_t i;
912912
for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) {
913913
if (!(*b11->msatoshi * 10 % multipliers[i].m10))
914914
break;

common/test/run-json.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ static int test_json_filter(void)
8282
static void test_json_escape(void)
8383
{
8484
int i;
85-
const char *str;
8685

8786
for (i = 1; i < 256; i++) {
8887
char badstr[2];
@@ -97,7 +96,7 @@ static void test_json_escape(void)
9796
json_add_escaped_string(result, "x", take(esc));
9897
json_object_end(result);
9998

100-
str = json_result_string(result);
99+
const char *str = json_result_string(result);
101100
if (i == '\\' || i == '"'
102101
|| i == '\n' || i == '\r' || i == '\b'
103102
|| i == '\t' || i == '\f')

gossipd/routing.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,6 @@ void routing_failure(struct routing_state *rstate,
13021302
const u8 *channel_update)
13031303
{
13041304
struct node *node;
1305-
int i;
13061305
time_t now = time_now().ts.tv_sec;
13071306

13081307
status_trace("Received routing failure 0x%04x (%s), "
@@ -1330,7 +1329,7 @@ void routing_failure(struct routing_state *rstate,
13301329
*
13311330
*/
13321331
if (failcode & NODE) {
1333-
for (i = 0; i < tal_count(node->chans); ++i) {
1332+
for (int i = 0; i < tal_count(node->chans); ++i) {
13341333
routing_failure_channel_out(tmpctx, node, failcode,
13351334
node->chans[i],
13361335
now);

hsmd/hsm.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,6 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
608608
struct pubkey local_pubkey, remote_pubkey;
609609
struct utxo **utxomap;
610610
struct bitcoin_tx *tx;
611-
u8 *wscript;
612611
u16 outnum;
613612
size_t i;
614613
struct pubkey changekey;
@@ -644,7 +643,7 @@ static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
644643
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
645644
else
646645
subscript = NULL;
647-
wscript = p2wpkh_scriptcode(tmpctx, &inkey);
646+
u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey);
648647

649648
sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey,
650649
&sig);
@@ -673,7 +672,6 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
673672
u64 satoshi_out, change_out;
674673
u32 change_keyindex;
675674
struct utxo **utxos;
676-
u8 *wscript;
677675
u8 **scriptSigs;
678676
struct bitcoin_tx *tx;
679677
struct ext_key ext;
@@ -715,7 +713,7 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
715713
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
716714
else
717715
subscript = NULL;
718-
wscript = p2wpkh_scriptcode(tmpctx, &inkey);
716+
u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey);
719717

720718
sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey,
721719
&sig);

lightningd/bitcoind.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,9 @@ static void fatal_bitcoind_failure(struct bitcoind *bitcoind, const char *error_
738738

739739
void wait_for_bitcoind(struct bitcoind *bitcoind)
740740
{
741-
int from, ret, status;
741+
int from, status;
742742
pid_t child;
743743
const char **cmd = cmdarr(bitcoind, bitcoind, "echo", NULL);
744-
char *output;
745744
bool printed = false;
746745

747746
for (;;) {
@@ -753,12 +752,12 @@ void wait_for_bitcoind(struct bitcoind *bitcoind)
753752
fatal("%s exec failed: %s", cmd[0], strerror(errno));
754753
}
755754

756-
output = grab_fd(cmd, from);
755+
char *output = grab_fd(cmd, from);
757756
if (!output)
758757
fatal("Reading from %s failed: %s",
759758
cmd[0], strerror(errno));
760759

761-
ret = waitpid(child, &status, 0);
760+
int ret = waitpid(child, &status, 0);
762761
if (ret != child)
763762
fatal("Waiting for %s: %s", cmd[0], strerror(errno));
764763
if (!WIFEXITED(status))

lightningd/channel.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,14 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...)
287287
struct lightningd *ld = channel->peer->ld;
288288
va_list ap;
289289
char *why;
290-
u8 *msg;
291290
struct channel_id cid;
292291

293292
va_start(ap, fmt);
294293
why = tal_vfmt(channel, fmt, ap);
295294
va_end(ap);
296295

297296
if (channel->scid) {
298-
msg = towire_gossip_disable_channel(NULL,
297+
u8 *msg = towire_gossip_disable_channel(NULL,
299298
channel->scid,
300299
channel->peer->direction,
301300
false);

lightningd/connect_control.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ static void json_connect(struct command *cmd,
111111
char *id_str;
112112
char *atptr;
113113
char *ataddr = NULL;
114-
int atidx;
115114
const char *name;
116115
struct wireaddr addr;
117116
u8 *msg;
@@ -130,7 +129,7 @@ static void json_connect(struct command *cmd,
130129
idtok->end - idtok->start);
131130
atptr = strchr(id_str, '@');
132131
if (atptr) {
133-
atidx = atptr - id_str;
132+
int atidx = atptr - id_str;
134133
ataddr = tal_strdup(cmd, atptr + 1);
135134
/* Cut id. */
136135
idtok->end = idtok->start + atidx;

lightningd/jsonrpc.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ bool json_get_params(struct command *cmd,
487487
const char **names;
488488
size_t num_names;
489489
/* Uninitialized warnings on p and end */
490-
const jsmntok_t **tokptr, *p = NULL, *end = NULL;
490+
const jsmntok_t *p = NULL, *end = NULL;
491491

492492
if (param->type == JSMN_ARRAY) {
493493
if (param->size == 0)
@@ -505,7 +505,7 @@ bool json_get_params(struct command *cmd,
505505
names = tal_arr(cmd, const char *, num_names + 1);
506506
va_start(ap, param);
507507
while ((names[num_names] = va_arg(ap, const char *)) != NULL) {
508-
tokptr = va_arg(ap, const jsmntok_t **);
508+
const jsmntok_t **tokptr = va_arg(ap, const jsmntok_t **);
509509
bool compulsory = true;
510510
if (names[num_names][0] == '?') {
511511
names[num_names]++;
@@ -788,7 +788,6 @@ json_tok_address_scriptpubkey(const tal_t *cxt,
788788
* of fixed size 40 will not overflow. */
789789
uint8_t witness_program[40];
790790
size_t witness_program_len;
791-
bool witness_ok;
792791

793792
char *addrz;
794793
const char *bip173;
@@ -826,7 +825,7 @@ json_tok_address_scriptpubkey(const tal_t *cxt,
826825
&witness_program_len, addrz);
827826

828827
if (bip173) {
829-
witness_ok = false;
828+
bool witness_ok = false;
830829
if (witness_version == 0 && (witness_program_len == 20 ||
831830
witness_program_len == 32)) {
832831
witness_ok = true;

lightningd/pay.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ void payment_store(struct lightningd *ld,
419419
void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
420420
const char *localfail)
421421
{
422-
struct onionreply *reply;
423-
struct secret *path_secrets;
424422
struct wallet_payment *payment;
425423
struct routing_failure* fail = NULL;
426424
const char *failmsg;
@@ -468,8 +466,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
468466
assert(!hout->failcode);
469467
failmsg = "reply from remote";
470468
/* Try to parse reply. */
471-
path_secrets = payment->path_secrets;
472-
reply = unwrap_onionreply(tmpctx, path_secrets,
469+
struct secret *path_secrets = payment->path_secrets;
470+
struct onionreply *reply = unwrap_onionreply(tmpctx, path_secrets,
473471
tal_count(path_secrets),
474472
hout->failuremsg);
475473
if (!reply) {
@@ -895,11 +893,10 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r,
895893
void *vcmd)
896894
{
897895
struct command *cmd = (struct command*) vcmd;
898-
struct json_result *response;
899896

900897
if (!r->succeeded && r->errorcode == PAY_IN_PROGRESS) {
901898
/* This is normal for sendpay. Succeed. */
902-
response = new_json_result(cmd);
899+
struct json_result *response = new_json_result(cmd);
903900
json_object_start(response, NULL);
904901
json_add_string(response, "message",
905902
"Monitor status with listpayments or waitsendpay");

0 commit comments

Comments
 (0)