From abf510740d93544dcb4719802ad4925455879915 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Wed, 25 Apr 2018 12:55:34 +0200 Subject: [PATCH] Force the use of the POSIX C locale for all commands and their subprocesses --- Makefile | 7 +++++-- bitcoin/test/run-tx-encode.c | 2 ++ channeld/channel.c | 2 ++ channeld/test/run-full_channel.c | 2 ++ cli/Makefile | 1 + cli/lightning-cli.c | 3 +++ cli/test/run-large-input.c | 3 ++- closingd/closing.c | 2 ++ common/test/run-bolt11.c | 2 ++ common/test/run-ip_port_parsing.c | 2 ++ common/test/run-json.c | 3 +++ common/test/run-sphinx.c | 2 ++ common/utils.c | 8 ++++++++ common/utils.h | 3 +++ devtools/bolt11-cli.c | 2 ++ devtools/decodemsg.c | 2 ++ devtools/onion.c | 2 ++ gossipd/gossip.c | 2 ++ gossipd/test/run-bench-find_route.c | 2 ++ gossipd/test/run-find_route-specific.c | 2 ++ gossipd/test/run-find_route.c | 2 ++ gossipd/test/run-initiator-success.c | 2 ++ gossipd/test/run-responder-success.c | 2 ++ hsmd/hsm.c | 2 ++ lightningd/lightningd.c | 2 ++ lightningd/test/run-commit_tx.c | 2 ++ lightningd/test/run-cryptomsg.c | 2 ++ lightningd/test/run-find_my_path.c | 2 ++ lightningd/test/run-funding_tx.c | 2 ++ lightningd/test/run-key_derive.c | 2 ++ onchaind/onchain.c | 2 ++ onchaind/test/run-grind_feerate.c | 2 ++ openingd/opening.c | 2 ++ tools/check-bolt.c | 3 +++ tools/check-setup_locale.sh | 16 ++++++++++++++++ wallet/test/run-db.c | 2 ++ wallet/test/run-wallet.c | 2 ++ wire/test/run-peer-wire.c | 2 ++ 38 files changed, 102 insertions(+), 3 deletions(-) create mode 100755 tools/check-setup_locale.sh diff --git a/Makefile b/Makefile index 7363c56fb..724873922 100644 --- a/Makefile +++ b/Makefile @@ -230,7 +230,7 @@ bolt-precheck: check-source-bolt: $(ALL_TEST_PROGRAMS:%=bolt-check/%.c) -tools/check-bolt: tools/check-bolt.o $(CCAN_OBJS) +tools/check-bolt: tools/check-bolt.o $(CCAN_OBJS) common/utils.o tools/check-bolt.o: $(CCAN_HEADERS) @@ -265,7 +265,10 @@ check-cppcheck: .cppcheck-suppress check-shellcheck: git ls-files -- "*.sh" | xargs shellcheck -check-source: check-makefile check-source-bolt check-whitespace check-markdown check-spelling check-python check-includes check-cppcheck check-shellcheck +check-setup_locale: + @tools/check-setup_locale.sh + +check-source: check-makefile check-source-bolt check-whitespace check-markdown check-spelling check-python check-includes check-cppcheck check-shellcheck check-setup_locale full-check: check check-source diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index 711f83226..4015d6087 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -26,6 +26,8 @@ static void tal_hexeq(const u8 *p, const char *hex) int main(void) { + setup_locale(); + struct bitcoin_tx *tx; tx = bitcoin_tx_from_hex(NULL, extended_tx, strlen(extended_tx)); diff --git a/channeld/channel.c b/channeld/channel.c index daf498a92..b7f678475 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -2544,6 +2544,8 @@ static void send_shutdown_complete(struct peer *peer) int main(int argc, char *argv[]) { + setup_locale(); + int i, nfds; fd_set fds_in, fds_out; struct peer *peer; diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index b022d3211..3f5242dce 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -317,6 +317,8 @@ static void update_feerate(struct channel *channel, u32 feerate) int main(void) { + setup_locale(); + struct bitcoin_txid funding_txid; /* We test from both sides. */ struct channel *lchannel, *rchannel; diff --git a/cli/Makefile b/cli/Makefile index e752e6bfa..8dbfe603d 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -5,6 +5,7 @@ LIGHTNING_CLI_COMMON_OBJS := \ common/configdir.o \ common/json.o \ common/json_escaped.o \ + common/utils.o \ common/version.o lightning-cli-all: cli/lightning-cli diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 5cc2d9606..b2b9e9f99 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -167,6 +168,8 @@ static void add_input(char **cmd, const char *input, int main(int argc, char *argv[]) { + setup_locale(); + int fd, i, off; const char *method; char *cmd, *resp, *idstr, *rpc_filename; diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index 2c1f06107..8ab26f2d3 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -83,8 +83,9 @@ ssize_t test_read(int fd UNUSED, void *buf, size_t len) int main(int argc UNUSED, char *argv[]) { - char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", NULL }; + setup_locale(); + char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", NULL }; /* sizeof() is an overestimate, but we don't care. */ response = tal_arr(NULL, char, diff --git a/closingd/closing.c b/closingd/closing.c index c4b2b8b93..d60cdbfb4 100644 --- a/closingd/closing.c +++ b/closingd/closing.c @@ -423,6 +423,8 @@ static u64 adjust_offer(struct crypto_state *cs, int main(int argc, char *argv[]) { + setup_locale(); + struct crypto_state cs; const tal_t *ctx = tal(NULL, char); u8 *msg; diff --git a/common/test/run-bolt11.c b/common/test/run-bolt11.c index aae727ad2..25471851d 100644 --- a/common/test/run-bolt11.c +++ b/common/test/run-bolt11.c @@ -115,6 +115,8 @@ static void test_b11(const char *b11str, int main(void) { + setup_locale(); + struct bolt11 *b11; struct pubkey node; u64 msatoshi; diff --git a/common/test/run-ip_port_parsing.c b/common/test/run-ip_port_parsing.c index 67e5017cd..fc8eae762 100644 --- a/common/test/run-ip_port_parsing.c +++ b/common/test/run-ip_port_parsing.c @@ -27,6 +27,8 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) int main(void) { + setup_locale(); + struct wireaddr addr; char *ip; u16 port; diff --git a/common/test/run-json.c b/common/test/run-json.c index ba2895886..956fdda35 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -1,5 +1,6 @@ #include "../json.c" #include "../json_escaped.c" +#include #include /* AUTOGENERATED MOCKS START */ @@ -146,6 +147,8 @@ static void test_json_partial(void) int main(void) { + setup_locale(); + test_json_tok_bitcoin_amount(); test_json_filter(); test_json_escape(); diff --git a/common/test/run-sphinx.c b/common/test/run-sphinx.c index 4ac6fc7fe..50d589d52 100644 --- a/common/test/run-sphinx.c +++ b/common/test/run-sphinx.c @@ -158,6 +158,8 @@ static void run_unit_tests(void) int main(int argc, char **argv) { + setup_locale(); + bool generate = false, decode = false, unit = false; u8 assocdata[32]; memset(assocdata, 'B', sizeof(assocdata)); diff --git a/common/utils.c b/common/utils.c index 602b42f19..e74da9798 100644 --- a/common/utils.c +++ b/common/utils.c @@ -2,6 +2,7 @@ #include #include #include +#include secp256k1_context *secp256k1_ctx; const tal_t *tmpctx; @@ -26,6 +27,13 @@ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len) return data; } +/* Use the POSIX C locale. */ +void setup_locale(void) +{ + setlocale(LC_ALL, "C"); + putenv("LC_ALL=C"); /* For exec{l,lp,v,vp}(...) */ +} + /* Global temporary convenience context: freed in io loop core. */ /* Initial creation of tmpctx. */ diff --git a/common/utils.h b/common/utils.h index 5411f51c8..dcf2cd201 100644 --- a/common/utils.h +++ b/common/utils.h @@ -16,6 +16,9 @@ char *tal_hex(const tal_t *ctx, const tal_t *data); /* Allocate and fill a buffer with the data of this hex string. */ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len); +/* Use the POSIX C locale. */ +void setup_locale(void); + /* Global temporary convenience context: freed in io loop core. */ extern const tal_t *tmpctx; diff --git a/devtools/bolt11-cli.c b/devtools/bolt11-cli.c index 4a703680d..8f3543a5f 100644 --- a/devtools/bolt11-cli.c +++ b/devtools/bolt11-cli.c @@ -54,6 +54,8 @@ static char *fmt_time(const tal_t *ctx, u64 time) int main(int argc, char *argv[]) { + setup_locale(); + const tal_t *ctx = tal(NULL, char); const char *method; struct bolt11 *b11; diff --git a/devtools/decodemsg.c b/devtools/decodemsg.c index 1e5a9c7c7..0524f18de 100644 --- a/devtools/decodemsg.c +++ b/devtools/decodemsg.c @@ -3,6 +3,8 @@ int main(int argc UNUSED, char *argv[]) { + setup_locale(); + u8 *m = tal_hexdata(NULL, argv[1], strlen(argv[1])); print_message(m); return 0; diff --git a/devtools/onion.c b/devtools/onion.c index b4aa0bbdd..1237a11d0 100644 --- a/devtools/onion.c +++ b/devtools/onion.c @@ -106,6 +106,8 @@ static void do_decode(int argc, char **argv) int main(int argc, char **argv) { + setup_locale(); + bool generate = false, decode = false; secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 30f73e4ac..5a73a5a7d 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -2362,6 +2362,8 @@ static void master_gone(struct io_conn *unused UNUSED, struct daemon_conn *dc UN int main(int argc, char *argv[]) { + setup_locale(); + struct daemon *daemon; subdaemon_setup(argc, argv); diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index b35ca9f4b..6a7f85c5a 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -203,6 +203,8 @@ static void run(const char *name) int main(int argc, char *argv[]) { + setup_locale(); + static const struct bitcoin_blkid zerohash; struct routing_state *rstate; size_t num_nodes = 100, num_runs = 1; diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index 0a7ff0dce..9a980327a 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -128,6 +128,8 @@ static bool channel_is_between(const struct chan *chan, int main(void) { + setup_locale(); + static const struct bitcoin_blkid zerohash; struct half_chan *nc; struct routing_state *rstate; diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index 6667250f6..243773dcf 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -172,6 +172,8 @@ static bool channel_is_between(const struct chan *chan, int main(void) { + setup_locale(); + static const struct bitcoin_blkid zerohash; struct routing_state *rstate; struct pubkey a, b, c, d; diff --git a/gossipd/test/run-initiator-success.c b/gossipd/test/run-initiator-success.c index a53529c74..3932deeac 100644 --- a/gossipd/test/run-initiator-success.c +++ b/gossipd/test/run-initiator-success.c @@ -199,6 +199,8 @@ bool hsm_do_ecdh(struct secret *ss, const struct pubkey *point) int main(void) { + setup_locale(); + struct wireaddr dummy; secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY diff --git a/gossipd/test/run-responder-success.c b/gossipd/test/run-responder-success.c index 235e662fb..a65c4c96f 100644 --- a/gossipd/test/run-responder-success.c +++ b/gossipd/test/run-responder-success.c @@ -196,6 +196,8 @@ bool hsm_do_ecdh(struct secret *ss, const struct pubkey *point) int main(void) { + setup_locale(); + struct wireaddr dummy; secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY diff --git a/hsmd/hsm.c b/hsmd/hsm.c index ae4b95262..25f80de42 100644 --- a/hsmd/hsm.c +++ b/hsmd/hsm.c @@ -824,6 +824,8 @@ static void master_gone(struct io_conn *unused UNUSED, struct daemon_conn *dc UN int main(int argc, char *argv[]) { + setup_locale(); + struct client *client; subdaemon_setup(argc, argv); diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index fdf079352..66c5c289a 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -280,6 +280,8 @@ static void pidfile_create(const struct lightningd *ld) int main(int argc, char *argv[]) { + setup_locale(); + struct lightningd *ld; bool newdir; u32 blockheight; diff --git a/lightningd/test/run-commit_tx.c b/lightningd/test/run-commit_tx.c index 6612fb29b..c4f6f2b03 100644 --- a/lightningd/test/run-commit_tx.c +++ b/lightningd/test/run-commit_tx.c @@ -425,6 +425,8 @@ static const struct htlc **invert_htlcs(const struct htlc **htlcs) int main(void) { + setup_locale(); + struct bitcoin_txid funding_txid; u64 funding_amount_satoshi, dust_limit_satoshi; u32 feerate_per_kw; diff --git a/lightningd/test/run-cryptomsg.c b/lightningd/test/run-cryptomsg.c index fe853cf37..95ca3d8d8 100644 --- a/lightningd/test/run-cryptomsg.c +++ b/lightningd/test/run-cryptomsg.c @@ -98,6 +98,8 @@ static struct secret secret_from_hex(const char *hex) int main(void) { + setup_locale(); + struct peer_crypto_state cs_out, cs_in; struct secret sk, rk, ck; const void *msg; diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index f10cddc54..f6a3db300 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -157,6 +157,8 @@ struct log *crashlog; #undef main int main(int argc UNUSED, char *argv[] UNUSED) { + setup_locale(); + char *argv0; /* We're assuming we're run from top build dir. */ const char *answer; diff --git a/lightningd/test/run-funding_tx.c b/lightningd/test/run-funding_tx.c index 37a766d60..934dc55c5 100644 --- a/lightningd/test/run-funding_tx.c +++ b/lightningd/test/run-funding_tx.c @@ -41,6 +41,8 @@ static struct privkey privkey_from_hex(const char *hex) int main(void) { + setup_locale(); + struct bitcoin_tx *input, *funding; u64 fee; struct pubkey local_funding_pubkey, remote_funding_pubkey; diff --git a/lightningd/test/run-key_derive.c b/lightningd/test/run-key_derive.c index be16c1666..8e02e7488 100644 --- a/lightningd/test/run-key_derive.c +++ b/lightningd/test/run-key_derive.c @@ -18,6 +18,8 @@ static struct secret secret_from_hex(const char *hex) int main(void) { + setup_locale(); + struct privkey privkey; struct secret base_secret, per_commitment_secret; struct pubkey base_point, per_commitment_point, pubkey, pubkey2; diff --git a/onchaind/onchain.c b/onchaind/onchain.c index 6fc65fcda..0a48eda14 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -2096,6 +2096,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, int main(int argc, char *argv[]) { + setup_locale(); + const tal_t *ctx = tal(NULL, char); u8 *msg; struct privkey seed; diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 7a2686a38..d67e5961a 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -153,6 +153,8 @@ bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED) int main(int argc, char *argv[]) { + setup_locale(); + struct bitcoin_tx *tx; secp256k1_ecdsa_signature sig; u8 *der, *wscript; diff --git a/openingd/opening.c b/openingd/opening.c index 8ca4aecf0..eb0c3ba56 100644 --- a/openingd/opening.c +++ b/openingd/opening.c @@ -793,6 +793,8 @@ static u8 *fundee_channel(struct state *state, #ifndef TESTING int main(int argc, char *argv[]) { + setup_locale(); + u8 *msg, *peer_msg; struct state *state = tal(NULL, struct state); struct privkey seed; diff --git a/tools/check-bolt.c b/tools/check-bolt.c index fd5c7e5ea..8fbbf7993 100644 --- a/tools/check-bolt.c +++ b/tools/check-bolt.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -256,6 +257,8 @@ static struct bolt_file *find_bolt(const char *bolt_prefix, int main(int argc, char *argv[]) { + setup_locale(); + struct bolt_file *bolts; int i; diff --git a/tools/check-setup_locale.sh b/tools/check-setup_locale.sh new file mode 100755 index 000000000..a4a39674f --- /dev/null +++ b/tools/check-setup_locale.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +EXIT_CODE=0 +for FILE in $(git grep -lE 'int main\(' | grep -vE '^ccan/'); do + if ! grep -q 'setup_locale();' "${FILE}"; then + echo "main(...) in ${FILE} does not call setup_locale() (see common/utils.h)" + EXIT_CODE=1 + fi +done +if [[ ${EXIT_CODE} != 0 ]]; then + echo + echo "setup_locale() forces the use of the POSIX C locale. By using the" + echo "POSIX C locale we avoid a class of localization related parsing bugs" + echo "that can be very tricky to isolate and fix." +fi +exit ${EXIT_CODE} diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 86d54292d..3979ade22 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -116,6 +116,8 @@ static bool test_vars(void) int main(void) { + setup_locale(); + bool ok = true; ok &= test_empty_db_migrate(); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 6672b90bc..152108346 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -992,6 +992,8 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx) int main(void) { + setup_locale(); + bool ok = true; struct lightningd *ld; diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index 422d27670..810a22976 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -844,6 +844,8 @@ static bool node_announcement_eq(const struct msg_node_announcement *a, int main(void) { + setup_locale(); + struct msg_channel_announcement ca, *ca2; struct msg_funding_locked fl, *fl2; struct msg_announcement_signatures as, *as2;