wallet_rpc_server: avoid repeated string allocations when parsing

This commit is contained in:
moneromooo-monero 2018-11-26 00:19:50 +00:00
parent 88c85c18e0
commit c4851024ce
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3

View file

@ -1684,23 +1684,14 @@ namespace tools
cryptonote::blobdata payment_id_blob; cryptonote::blobdata payment_id_blob;
// TODO - should the whole thing fail because of one bad id? // TODO - should the whole thing fail because of one bad id?
bool r;
if(!epee::string_tools::parse_hexstr_to_binbuff(payment_id_str, payment_id_blob)) if (payment_id_str.size() == 2 * sizeof(payment_id))
{ {
er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; r = epee::string_tools::hex_to_pod(payment_id_str, payment_id);
er.message = "Payment ID has invalid format: " + payment_id_str;
return false;
} }
else if (payment_id_str.size() == 2 * sizeof(payment_id8))
if(sizeof(payment_id) == payment_id_blob.size())
{ {
payment_id = *reinterpret_cast<const crypto::hash*>(payment_id_blob.data()); r = epee::string_tools::hex_to_pod(payment_id_str, payment_id8);
}
else if(sizeof(payment_id8) == payment_id_blob.size())
{
payment_id8 = *reinterpret_cast<const crypto::hash8*>(payment_id_blob.data());
memcpy(payment_id.data, payment_id8.data, 8);
memset(payment_id.data + 8, 0, 24);
} }
else else
{ {
@ -1709,6 +1700,13 @@ namespace tools
return false; return false;
} }
if(!r)
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID;
er.message = "Payment ID has invalid format: " + payment_id_str;
return false;
}
std::list<wallet2::payment_details> payment_list; std::list<wallet2::payment_details> payment_list;
m_wallet->get_payments(payment_id, payment_list, req.min_block_height); m_wallet->get_payments(payment_id, payment_list, req.min_block_height);
@ -2588,23 +2586,19 @@ namespace tools
ski.resize(req.signed_key_images.size()); ski.resize(req.signed_key_images.size());
for (size_t n = 0; n < ski.size(); ++n) for (size_t n = 0; n < ski.size(); ++n)
{ {
cryptonote::blobdata bd; if (!epee::string_tools::hex_to_pod(req.signed_key_images[n].key_image, ski[n].first))
if(!epee::string_tools::parse_hexstr_to_binbuff(req.signed_key_images[n].key_image, bd) || bd.size() != sizeof(crypto::key_image))
{ {
er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE; er.code = WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE;
er.message = "failed to parse key image"; er.message = "failed to parse key image";
return false; return false;
} }
ski[n].first = *reinterpret_cast<const crypto::key_image*>(bd.data());
if(!epee::string_tools::parse_hexstr_to_binbuff(req.signed_key_images[n].signature, bd) || bd.size() != sizeof(crypto::signature)) if (!epee::string_tools::hex_to_pod(req.signed_key_images[n].signature, ski[n].second))
{ {
er.code = WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE; er.code = WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE;
er.message = "failed to parse signature"; er.message = "failed to parse signature";
return false; return false;
} }
ski[n].second = *reinterpret_cast<const crypto::signature*>(bd.data());
} }
uint64_t spent = 0, unspent = 0; uint64_t spent = 0, unspent = 0;
uint64_t height = m_wallet->import_key_images(ski, req.offset, spent, unspent); uint64_t height = m_wallet->import_key_images(ski, req.offset, spent, unspent);