From c161ddb66a52ca3db53620e9c44e90deb7980ed9 Mon Sep 17 00:00:00 2001 From: Arkady Paronyan Date: Fri, 3 Oct 2014 17:53:04 +0400 Subject: [PATCH] fixed segfaults on accessing temp objects vector buffers --- alethzero/MainWin.cpp | 5 +++-- libdevcore/RLP.h | 3 +++ libdevcrypto/TrieDB.h | 26 +++++++++++++++++++------- libethereum/BlockChain.cpp | 9 ++++++--- libethereum/Client.cpp | 4 +++- test/crypto.cpp | 20 ++++++++++++-------- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 2181098a9..c7f4f7e10 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -109,8 +109,9 @@ Main::Main(QWidget *parent) : cerr << "State root: " << BlockChain::genesis().stateRoot << endl; cerr << "Block Hash: " << sha3(BlockChain::createGenesisBlock()) << endl; - cerr << "Block RLP: " << RLP(BlockChain::createGenesisBlock()) << endl; - cerr << "Block Hex: " << toHex(BlockChain::createGenesisBlock()) << endl; + auto block = BlockChain::createGenesisBlock(); + cerr << "Block RLP: " << RLP(block) << endl; + cerr << "Block Hex: " << toHex(BlockChain::createGenesisBlock()) << endl; cerr << "Network protocol version: " << dev::eth::c_protocolVersion << endl; cerr << "Client database version: " << dev::eth::c_databaseVersion << endl; diff --git a/libdevcore/RLP.h b/libdevcore/RLP.h index 35e1d1dcf..4e45eda25 100644 --- a/libdevcore/RLP.h +++ b/libdevcore/RLP.h @@ -307,6 +307,9 @@ private: mutable unsigned m_lastIndex = (unsigned)-1; mutable unsigned m_lastEnd = 0; mutable bytesConstRef m_lastItem; + + /// Disable construction from rvalue + explicit RLP(bytes const&& _d) {} }; /** diff --git a/libdevcrypto/TrieDB.h b/libdevcrypto/TrieDB.h index 8ee6f3bca..68b819404 100644 --- a/libdevcrypto/TrieDB.h +++ b/libdevcrypto/TrieDB.h @@ -552,12 +552,18 @@ template bytes GenericTrieDB::mergeAt(RLP const& _orig, NibbleSli auto sh = _k.shared(k); // std::cout << _k << " sh " << k << " = " << sh << std::endl; - if (sh) + if (sh) + { // shared stuff - cleve at disagreement. - return mergeAt(RLP(cleve(_orig, sh)), _k, _v, true); + auto cleved = cleve(_orig, sh); + return mergeAt(RLP(cleved), _k, _v, true); + } else + { // nothing shared - branch - return mergeAt(RLP(branch(_orig)), _k, _v, true); + auto branched = branch(_orig); + return mergeAt(RLP(branched), _k, _v, true); + } } else { @@ -685,8 +691,11 @@ template bytes GenericTrieDB::deleteAt(RLP const& _orig, NibbleSl byte used = uniqueInUse(_orig, 16); if (used != 255) - if (isTwoItemNode(_orig[used])) - return graft(RLP(merge(_orig, used))); + if (isTwoItemNode(_orig[used])) + { + auto merged = merge(_orig, used); + return graft(RLP(merged)); + } else return merge(_orig, used); else @@ -721,8 +730,11 @@ template bytes GenericTrieDB::deleteAt(RLP const& _orig, NibbleSl return r.out(); // yes; merge - if (isTwoItemNode(rlp[used])) - return graft(RLP(merge(rlp, used))); + if (isTwoItemNode(rlp[used])) + { + auto merged = merge(rlp, used); + return graft(RLP(merged)); + } else return merge(rlp, used); } diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index f375adfcb..c8646c2db 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -277,8 +277,10 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) auto pd = details(bi.parentHash); if (!pd) { - cwarn << "Odd: details is returning false despite block known:" << RLP(pd.rlp()); - cwarn << "Block:" << RLP(block(bi.parentHash)); + auto pdata = pd.rlp(); + cwarn << "Odd: details is returning false despite block known:" << RLP(pdata); + auto parentBlock = block(bi.parentHash); + cwarn << "Block:" << RLP(parentBlock); } // Check it's not crazy @@ -447,7 +449,8 @@ h256Set BlockChain::allUnclesFrom(h256 _parent) const for (unsigned i = 0; i < 6 && p != m_genesisHash; ++i, p = details(p).parent) { ret.insert(p); // TODO: check: should this be details(p).parent? - for (auto i: RLP(block(p))[2]) + auto bl = block(p); + for (auto i: RLP(bl)[2]) ret.insert(sha3(i.data())); } return ret; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index de0077f89..b38b1db8e 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -36,7 +36,9 @@ using namespace p2p; VersionChecker::VersionChecker(string const& _dbPath): m_path(_dbPath.size() ? _dbPath : Defaults::dbPath()) { - m_ok = RLP(contents(m_path + "/protocol")).toInt(RLP::LaisezFaire) == c_protocolVersion && RLP(contents(m_path + "/database")).toInt(RLP::LaisezFaire) == c_databaseVersion; + auto protocolContents = contents(m_path + "/protocol"); + auto databaseContents = contents(m_path + "/database"); + m_ok = RLP(protocolContents).toInt(RLP::LaisezFaire) == c_protocolVersion && RLP(databaseContents).toInt(RLP::LaisezFaire) == c_databaseVersion; } void VersionChecker::setOk() diff --git a/test/crypto.cpp b/test/crypto.cpp index d2943984e..55feb1a54 100644 --- a/test/crypto.cpp +++ b/test/crypto.cpp @@ -46,12 +46,14 @@ BOOST_AUTO_TEST_CASE(crypto_tests) t.nonce = 0; t.receiveAddress = h160(fromHex("944400f4b88ac9589a0f17ed4671da26bddb668b")); t.value = 1000; - cnote << RLP(t.rlp(false)); - cnote << toHex(t.rlp(false)); + auto rlp = t.rlp(false); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(false); t.sign(p.secret()); - cnote << RLP(t.rlp(true)); - cnote << toHex(t.rlp(true)); + rlp = t.rlp(true); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(true); BOOST_REQUIRE(t.sender() == p.address()); } @@ -72,12 +74,14 @@ int cryptoTest() t.nonce = 0; t.receiveAddress = h160(fromHex("944400f4b88ac9589a0f17ed4671da26bddb668b")); t.value = 1000; - cnote << RLP(t.rlp(false)); - cnote << toHex(t.rlp(false)); + auto rlp = t.rlp(false); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(false); t.sign(p.secret()); - cnote << RLP(t.rlp(true)); - cnote << toHex(t.rlp(true)); + rlp = t.rlp(true); + cnote << RLP(rlp); + cnote << toHex(rlp); cnote << t.sha3(true); assert(t.sender() == p.address()); }