diff options
author | Riccardo Spagni <ric@spagni.net> | 2017-11-25 19:53:11 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2017-11-25 19:53:12 +0200 |
commit | 4c90d638bc9787596dbb40e4dc6ce4cc3909fa3a (patch) | |
tree | b8f3200e977879f3eedf4ef565584e71ca22719c /src | |
parent | Merge pull request #2822 (diff) | |
parent | Add out-of-bound exceptions and handle them in RPC (diff) | |
download | monero-4c90d638bc9787596dbb40e4dc6ce4cc3909fa3a.tar.xz |
Merge pull request #2823
0d149f70 Add out-of-bound exceptions and handle them in RPC (Michał Sałaban)
Diffstat (limited to 'src')
-rw-r--r-- | src/wallet/wallet2.cpp | 10 | ||||
-rw-r--r-- | src/wallet/wallet_errors.h | 22 | ||||
-rw-r--r-- | src/wallet/wallet_rpc_server.cpp | 42 |
3 files changed, 47 insertions, 27 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index fdaac5e70..6c9778532 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -699,8 +699,7 @@ void wallet2::add_subaddress_account(const std::string& label) //---------------------------------------------------------------------------------------------------- void wallet2::add_subaddress(uint32_t index_major, const std::string& label) { - if (index_major >= m_subaddress_labels.size()) - throw std::runtime_error("index_major is out of bound"); + THROW_WALLET_EXCEPTION_IF(index_major >= m_subaddress_labels.size(), error::account_index_outofbound); uint32_t index_minor = (uint32_t)get_num_subaddresses(index_major); expand_subaddresses({index_major, index_minor}); m_subaddress_labels[index_major][index_minor] = label; @@ -756,10 +755,9 @@ std::string wallet2::get_subaddress_label(const cryptonote::subaddress_index& in //---------------------------------------------------------------------------------------------------- void wallet2::set_subaddress_label(const cryptonote::subaddress_index& index, const std::string &label) { - if (index.major >= m_subaddress_labels.size() || index.minor >= m_subaddress_labels[index.major].size()) - MERROR("Subaddress index is out of bounds. Failed to set subaddress label."); - else - m_subaddress_labels[index.major][index.minor] = label; + THROW_WALLET_EXCEPTION_IF(index.major >= m_subaddress_labels.size(), error::account_index_outofbound); + THROW_WALLET_EXCEPTION_IF(index.minor >= m_subaddress_labels[index.major].size(), error::address_index_outofbound); + m_subaddress_labels[index.major][index.minor] = label; } //---------------------------------------------------------------------------------------------------- /*! diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index 41eb77451..48fce40dd 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -269,6 +269,28 @@ namespace tools } }; //---------------------------------------------------------------------------------------------------- + struct index_outofbound : public wallet_logic_error + { + explicit index_outofbound(std::string&& loc, const std::string& message) + : wallet_logic_error(std::move(loc), message) + { + } + }; + struct account_index_outofbound : public index_outofbound + { + explicit account_index_outofbound(std::string&& loc) + : index_outofbound(std::move(loc), "account index is out of bound") + { + } + }; + struct address_index_outofbound: public index_outofbound + { + explicit address_index_outofbound(std::string&& loc) + : index_outofbound(std::move(loc), "address index is out of bound") + { + } + }; + //---------------------------------------------------------------------------------------------------- struct acc_outs_lookup_error : public refresh_error { explicit acc_outs_lookup_error(std::string&& loc, const cryptonote::transaction& tx, diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index fe7f51617..c315684de 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -378,27 +378,23 @@ namespace tools bool wallet_rpc_server::on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er) { if (!m_wallet) return not_open(er); - m_wallet->add_subaddress(req.account_index, req.label); - res.address_index = m_wallet->get_num_subaddresses(req.account_index) - 1; - res.address = m_wallet->get_subaddress_as_str({req.account_index, res.address_index}); + try + { + m_wallet->add_subaddress(req.account_index, req.label); + res.address_index = m_wallet->get_num_subaddresses(req.account_index) - 1; + res.address = m_wallet->get_subaddress_as_str({req.account_index, res.address_index}); + } + catch (const std::exception& e) + { + handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); + return false; + } return true; } //------------------------------------------------------------------------------------------------------------------------------ bool wallet_rpc_server::on_label_address(const wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::response& res, epee::json_rpc::error& er) { if (!m_wallet) return not_open(er); - if (req.index.major >= m_wallet->get_num_subaddress_accounts()) - { - er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; - er.message = "Account index is out of bound"; - return false; - } - if (req.index.minor >= m_wallet->get_num_subaddresses(req.index.major)) - { - er.code = WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUTOFBOUND; - er.message = "Address index is out of bound"; - return false; - } try { m_wallet->set_subaddress_label(req.index, req.label); @@ -460,12 +456,6 @@ namespace tools bool wallet_rpc_server::on_label_account(const wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT::request& req, wallet_rpc::COMMAND_RPC_LABEL_ACCOUNT::response& res, epee::json_rpc::error& er) { if (!m_wallet) return not_open(er); - if (req.account_index >= m_wallet->get_num_subaddress_accounts()) - { - er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; - er.message = "Account index is out of bound"; - return false; - } try { m_wallet->set_subaddress_label({req.account_index, 0}, req.label); @@ -2325,6 +2315,16 @@ namespace tools er.code = WALLET_RPC_ERROR_CODE_INVALID_PASSWORD; er.message = "Invalid password."; } + catch (const error::account_index_outofbound& e) + { + er.code = WALLET_RPC_ERROR_CODE_ACCOUNT_INDEX_OUTOFBOUND; + er.message = e.what(); + } + catch (const error::address_index_outofbound& e) + { + er.code = WALLET_RPC_ERROR_CODE_ADDRESS_INDEX_OUTOFBOUND; + er.message = e.what(); + } catch (const std::exception& e) { er.code = default_error_code; |