From f3724aef881ff2fdada98eddc792059e4e18c1c8 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sat, 21 Nov 2015 10:13:10 +0000 Subject: [PATCH] Fix startup crash when using a locale boost does not like There are various locale related bugs in various versions of boost, where exceptions are thrown in boost::filesystem APIs when the current locale is not to boost's liking. It's not clear what "not to boost's liking" means in detail, though "en" and "en_US.UTF-8" are not to its liking. Fix it by running a test function that's known to throw in such a case, and resetting LANG and LC_ALL to C if an exception is thrown. In simplewallet, the locale is queried before that so the correct translations will still be used. --- src/blockchain_utilities/blockchain_dump.cpp | 2 ++ .../blockchain_export.cpp | 2 ++ .../blockchain_import.cpp | 2 ++ src/common/util.cpp | 19 +++++++++++++++++++ src/common/util.h | 2 ++ src/daemon/main.cpp | 2 ++ src/simplewallet/simplewallet.cpp | 5 ++++- 7 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/blockchain_utilities/blockchain_dump.cpp b/src/blockchain_utilities/blockchain_dump.cpp index 1c3c94636..f3666c72b 100644 --- a/src/blockchain_utilities/blockchain_dump.cpp +++ b/src/blockchain_utilities/blockchain_dump.cpp @@ -94,6 +94,8 @@ int main(int argc, char* argv[]) uint32_t log_level = 0; uint64_t block_stop = 0; + tools::sanitize_locale(); + boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; boost::filesystem::path output_file_path; diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp index 1bdaa3d7e..bd463b14e 100644 --- a/src/blockchain_utilities/blockchain_export.cpp +++ b/src/blockchain_utilities/blockchain_export.cpp @@ -42,6 +42,8 @@ int main(int argc, char* argv[]) uint64_t block_stop = 0; bool blocks_dat = false; + tools::sanitize_locale(); + boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; boost::filesystem::path output_file_path; diff --git a/src/blockchain_utilities/blockchain_import.cpp b/src/blockchain_utilities/blockchain_import.cpp index 88d748411..f8158ff2f 100644 --- a/src/blockchain_utilities/blockchain_import.cpp +++ b/src/blockchain_utilities/blockchain_import.cpp @@ -538,6 +538,8 @@ int main(int argc, char* argv[]) std::string m_config_folder; std::string db_arg_str; + tools::sanitize_locale(); + boost::filesystem::path default_data_path {tools::get_default_data_dir()}; boost::filesystem::path default_testnet_data_path {default_data_path / "testnet"}; std::string import_file_path; diff --git a/src/common/util.cpp b/src/common/util.cpp index 7d39bc4f4..4c1b44004 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -43,6 +43,7 @@ using namespace epee; #else #include #endif +#include namespace tools @@ -389,4 +390,22 @@ std::string get_nix_version_display_string() #endif return std::error_code(code, std::system_category()); } + + bool sanitize_locale() + { + // boost::filesystem throws for "invalid" locales, such as en_US.UTF-8, or kjsdkfs, + // so reset it here before any calls to it + try + { + boost::filesystem::path p {std::string("test")}; + p /= std::string("test"); + } + catch (...) + { + setenv("LC_ALL", "C", 1); + setenv("LANG", "C", 1); + return true; + } + return false; + } } diff --git a/src/common/util.h b/src/common/util.h index 883fe1e0f..236a0b6f0 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -88,6 +88,8 @@ namespace tools */ std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name); + bool sanitize_locale(); + inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot) { std::string s; diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 547f4bd9e..a8369f98d 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -54,6 +54,8 @@ int main(int argc, char const * argv[]) _note_c("dbg/main", "Begin of main()"); // TODO parse the debug options like set log level right here at start + tools::sanitize_locale(); + epee::string_tools::set_module_name_and_folder(argv[0]); // Build argument description diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 3d7a23d5e..3e1ca4210 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -2164,6 +2164,9 @@ int main(int argc, char* argv[]) //TRY_ENTRY(); + std::string lang = i18n_get_language(); + tools::sanitize_locale(); + string_tools::set_module_name_and_folder(argv[0]); po::options_description desc_general(sw::tr("General options")); @@ -2212,7 +2215,7 @@ int main(int argc, char* argv[]) po::positional_options_description positional_options; positional_options.add(arg_command.name, -1); - i18n_set_language("translations", "monero"); + i18n_set_language("translations", "monero", lang); po::options_description desc_all; desc_all.add(desc_general).add(desc_params);