From 12ac2daef0dfca631ecef26b377f915b7d9ff4c0 Mon Sep 17 00:00:00 2001 From: Bart Wyatt Date: Tue, 18 Dec 2018 16:21:37 -0500 Subject: [PATCH 1/2] use `assign` instead of `resize`+`memcpy` to update `shared_blob` data Co-Authored-By: Kayan --- libraries/chain/apply_context.cpp | 6 ++---- libraries/chain/eosio_contract.cpp | 17 ++++++++++------- libraries/chain/include/eosio/chain/types.hpp | 14 +++++++++++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index bedf5b4d616..1beb647eed6 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -521,9 +521,8 @@ int apply_context::db_store_i64( uint64_t code, uint64_t scope, uint64_t table, const auto& obj = db.create( [&]( auto& o ) { o.t_id = tableid; o.primary_key = id; - o.value.resize( buffer_size ); + o.value.assign( buffer, buffer_size ); o.payer = payer; - memcpy( o.value.data(), buffer, buffer_size ); }); db.modify( tab, [&]( auto& t ) { @@ -562,8 +561,7 @@ void apply_context::db_update_i64( int iterator, account_name payer, const char* } db.modify( obj, [&]( auto& o ) { - o.value.resize( buffer_size ); - memcpy( o.value.data(), buffer, buffer_size ); + o.value.assign( buffer, buffer_size ); o.payer = payer; }); } diff --git a/libraries/chain/eosio_contract.cpp b/libraries/chain/eosio_contract.cpp index 8d1a8dc7562..03e0fed7f7f 100644 --- a/libraries/chain/eosio_contract.cpp +++ b/libraries/chain/eosio_contract.cpp @@ -155,10 +155,11 @@ void apply_eosio_setcode(apply_context& context) { // TODO: update setcode message to include the hash, then validate it in validate a.last_code_update = context.control.pending_block_time(); a.code_version = code_id; - a.code.resize( code_size ); - if( code_size > 0 ) - memcpy( a.code.data(), act.code.data(), code_size ); - + if ( code_size > 0 ) { + a.code.assign(act.code.data(), code_size); + } else { + a.code.resize(0); + } }); const auto& account_sequence = db.get(act.account); @@ -185,9 +186,11 @@ void apply_eosio_setabi(apply_context& context) { int64_t new_size = abi_size; db.modify( account, [&]( auto& a ) { - a.abi.resize( abi_size ); - if( abi_size > 0 ) - memcpy( a.abi.data(), act.abi.data(), abi_size ); + if (abi_size > 0) { + a.abi.assign(act.abi.data(), abi_size); + } else { + a.abi.resize(0); + } }); const auto& account_sequence = db.get(act.account); diff --git a/libraries/chain/include/eosio/chain/types.hpp b/libraries/chain/include/eosio/chain/types.hpp index 9319a853258..d5fec0abf97 100644 --- a/libraries/chain/include/eosio/chain/types.hpp +++ b/libraries/chain/include/eosio/chain/types.hpp @@ -103,7 +103,19 @@ namespace eosio { namespace chain { */ class shared_blob : public shared_string { public: - shared_blob() = default; + shared_blob() = delete; + shared_blob(shared_blob&&) = default; + + shared_blob(const shared_blob& s) + :shared_string(s.get_allocator()) + { + assign(s.c_str(), s.size()); + } + + shared_blob &operator=(const shared_blob &s) { + assign(s.c_str(), s.size()); + return *this; + } template shared_blob(InputIterator f, InputIterator l, const allocator_type& a) From d02741c09f28a23ef3ca627ed21aa61d5a45f7f3 Mon Sep 17 00:00:00 2001 From: Bart Wyatt Date: Tue, 18 Dec 2018 17:13:26 -0500 Subject: [PATCH 2/2] add explicitly defaulted move semantics --- libraries/chain/include/eosio/chain/types.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/chain/include/eosio/chain/types.hpp b/libraries/chain/include/eosio/chain/types.hpp index d5fec0abf97..21fbf216c43 100644 --- a/libraries/chain/include/eosio/chain/types.hpp +++ b/libraries/chain/include/eosio/chain/types.hpp @@ -112,11 +112,14 @@ namespace eosio { namespace chain { assign(s.c_str(), s.size()); } - shared_blob &operator=(const shared_blob &s) { + + shared_blob& operator=(const shared_blob& s) { assign(s.c_str(), s.size()); return *this; } + shared_blob& operator=(shared_blob&& ) = default; + template shared_blob(InputIterator f, InputIterator l, const allocator_type& a) :shared_string(f,l,a)