Skip to content

Commit

Permalink
Reduce variable scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalswift authored and rustyrussell committed Mar 26, 2018
1 parent 94ca824 commit 7e9750f
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 60 deletions.
5 changes: 2 additions & 3 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions common/bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions common/test/run-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions gossipd/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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), "
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions hsmd/hsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (;;) {
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,14 @@ 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);
why = tal_vfmt(channel, fmt, ap);
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);
Expand Down
3 changes: 1 addition & 2 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]++;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 3 additions & 6 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 2 additions & 3 deletions lightningd/payalgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions wallet/txfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
6 changes: 2 additions & 4 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,14 @@ 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);

ok = stmt != NULL && sqlite3_step(stmt) == SQLITE_ROW;
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);
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 7e9750f

Please sign in to comment.