Merge pull request #2683

105425b7 simplewallet: reject invalid argument for boolean parameter (stoffu)
This commit is contained in:
Riccardo Spagni 2017-11-14 15:04:15 +02:00
commit f4fded6fcf
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
3 changed files with 95 additions and 26 deletions

View file

@ -78,6 +78,20 @@ namespace command_line
return false; return false;
} }
bool is_no(const std::string& str)
{
if (str == "n" || str == "N")
return true;
boost::algorithm::is_iequal ignore_case{};
if (boost::algorithm::equals("no", str, ignore_case))
return true;
if (boost::algorithm::equals(command_line::tr("no"), str, ignore_case))
return true;
return false;
}
const arg_descriptor<bool> arg_help = {"help", "Produce help message"}; const arg_descriptor<bool> arg_help = {"help", "Produce help message"};
const arg_descriptor<bool> arg_version = {"version", "Output version information"}; const arg_descriptor<bool> arg_version = {"version", "Output version information"};
const arg_descriptor<std::string> arg_data_dir = {"data-dir", "Specify data directory"}; const arg_descriptor<std::string> arg_data_dir = {"data-dir", "Specify data directory"};

View file

@ -45,6 +45,8 @@ namespace command_line
//! \return True if `str` is `is_iequal("y" || "yes" || `tr("yes"))`. //! \return True if `str` is `is_iequal("y" || "yes" || `tr("yes"))`.
bool is_yes(const std::string& str); bool is_yes(const std::string& str);
//! \return True if `str` is `is_iequal("n" || "no" || `tr("no"))`.
bool is_no(const std::string& str);
template<typename T, bool required = false> template<typename T, bool required = false>
struct arg_descriptor; struct arg_descriptor;

View file

@ -162,20 +162,50 @@ namespace
return tools::scoped_message_writer(console_color_red, true, sw::tr("Error: "), el::Level::Error); return tools::scoped_message_writer(console_color_red, true, sw::tr("Error: "), el::Level::Error);
} }
bool is_it_true(const std::string& s) bool parse_bool(const std::string& s, bool& result)
{ {
if (s == "1" || command_line::is_yes(s)) if (s == "1" || command_line::is_yes(s))
{
result = true;
return true; return true;
}
if (s == "0" || command_line::is_no(s))
{
result = false;
return true;
}
boost::algorithm::is_iequal ignore_case{}; boost::algorithm::is_iequal ignore_case{};
if (boost::algorithm::equals("true", s, ignore_case)) if (boost::algorithm::equals("true", s, ignore_case) || boost::algorithm::equals(simple_wallet::tr("true"), s, ignore_case))
{
result = true;
return true; return true;
if (boost::algorithm::equals(simple_wallet::tr("true"), s, ignore_case)) }
if (boost::algorithm::equals("false", s, ignore_case) || boost::algorithm::equals(simple_wallet::tr("false"), s, ignore_case))
{
result = false;
return true; return true;
}
return false; return false;
} }
template <typename F>
bool parse_bool_and_use(const std::string& s, F func)
{
bool r;
if (parse_bool(s, r))
{
func(r);
return true;
}
else
{
fail_msg_writer() << tr("invalid argument: must be either 0/1, true/false, y/n, yes/no");
return false;
}
}
const struct const struct
{ {
const char *name; const char *name;
@ -508,8 +538,10 @@ bool simple_wallet::set_always_confirm_transfers(const std::vector<std::string>
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->always_confirm_transfers(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->always_confirm_transfers(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -519,8 +551,10 @@ bool simple_wallet::set_print_ring_members(const std::vector<std::string> &args/
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->print_ring_members(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->print_ring_members(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -536,8 +570,10 @@ bool simple_wallet::set_store_tx_info(const std::vector<std::string> &args/* = s
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->store_tx_info(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->store_tx_info(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -632,7 +668,7 @@ bool simple_wallet::set_auto_refresh(const std::vector<std::string> &args/* = st
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
const bool auto_refresh = is_it_true(args[1]); parse_bool_and_use(args[1], [&](bool auto_refresh) {
m_wallet->auto_refresh(auto_refresh); m_wallet->auto_refresh(auto_refresh);
m_idle_mutex.lock(); m_idle_mutex.lock();
m_auto_refresh_enabled.store(auto_refresh, std::memory_order_relaxed); m_auto_refresh_enabled.store(auto_refresh, std::memory_order_relaxed);
@ -640,6 +676,7 @@ bool simple_wallet::set_auto_refresh(const std::vector<std::string> &args/* = st
m_idle_mutex.unlock(); m_idle_mutex.unlock();
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -666,8 +703,10 @@ bool simple_wallet::set_confirm_missing_payment_id(const std::vector<std::string
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->confirm_missing_payment_id(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->confirm_missing_payment_id(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -677,8 +716,10 @@ bool simple_wallet::set_ask_password(const std::vector<std::string> &args/* = st
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->ask_password(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->ask_password(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -754,8 +795,10 @@ bool simple_wallet::set_merge_destinations(const std::vector<std::string> &args/
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->merge_destinations(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->merge_destinations(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -765,8 +808,10 @@ bool simple_wallet::set_confirm_backlog(const std::vector<std::string> &args/* =
const auto pwd_container = get_and_verify_password(); const auto pwd_container = get_and_verify_password();
if (pwd_container) if (pwd_container)
{ {
m_wallet->confirm_backlog(is_it_true(args[1])); parse_bool_and_use(args[1], [&](bool r) {
m_wallet->confirm_backlog(r);
m_wallet->rewrite(m_wallet_file, pwd_container->password()); m_wallet->rewrite(m_wallet_file, pwd_container->password());
});
} }
return true; return true;
} }
@ -1926,8 +1971,16 @@ bool simple_wallet::start_mining(const std::vector<std::string>& args)
bool ok = true; bool ok = true;
size_t max_mining_threads_count = (std::max)(tools::get_max_concurrency(), static_cast<unsigned>(2)); size_t max_mining_threads_count = (std::max)(tools::get_max_concurrency(), static_cast<unsigned>(2));
size_t arg_size = args.size(); size_t arg_size = args.size();
if(arg_size >= 3) req.ignore_battery = is_it_true(args[2]); if(arg_size >= 3)
if(arg_size >= 2) req.do_background_mining = is_it_true(args[1]); {
if (!parse_bool_and_use(args[2], [&](bool r) { req.ignore_battery = r; }))
return true;
}
if(arg_size >= 2)
{
if (!parse_bool_and_use(args[1], [&](bool r) { req.do_background_mining = r; }))
return true;
}
if(arg_size >= 1) if(arg_size >= 1)
{ {
uint16_t num = 1; uint16_t num = 1;