Avoid dereference-of-casted-pointer

This commit is contained in:
Pieter Wuille 2017-07-07 00:54:42 -07:00
parent 5af6572534
commit 0aadc11fd8
8 changed files with 26 additions and 18 deletions

View file

@ -10,7 +10,7 @@
#include "memusage.h" #include "memusage.h"
static inline size_t RecursiveDynamicUsage(const CScript& script) { static inline size_t RecursiveDynamicUsage(const CScript& script) {
return memusage::DynamicUsage(*static_cast<const CScriptBase*>(&script)); return memusage::DynamicUsage(script);
} }
static inline size_t RecursiveDynamicUsage(const COutPoint& out) { static inline size_t RecursiveDynamicUsage(const COutPoint& out) {

View file

@ -107,7 +107,7 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) { inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(prevout); READWRITE(prevout);
READWRITE(*(CScriptBase*)(&scriptSig)); READWRITE(scriptSig);
READWRITE(nSequence); READWRITE(nSequence);
} }
@ -147,7 +147,7 @@ public:
template <typename Stream, typename Operation> template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) { inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nValue); READWRITE(nValue);
READWRITE(*(CScriptBase*)(&scriptPubKey)); READWRITE(scriptPubKey);
} }
void SetNull() void SetNull()

View file

@ -798,7 +798,7 @@ static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash,
stats.nTransactions++; stats.nTransactions++;
for (const auto output : outputs) { for (const auto output : outputs) {
ss << VARINT(output.first + 1); ss << VARINT(output.first + 1);
ss << *(const CScriptBase*)(&output.second.out.scriptPubKey); ss << output.second.out.scriptPubKey;
ss << VARINT(output.second.out.nValue); ss << VARINT(output.second.out.nValue);
stats.nTransactionOutputs++; stats.nTransactionOutputs++;
stats.nTotalAmount += output.second.out.nValue; stats.nTotalAmount += output.second.out.nValue;

View file

@ -1099,7 +1099,7 @@ public:
// Serialize the script // Serialize the script
if (nInput != nIn) if (nInput != nIn)
// Blank out other inputs' signatures // Blank out other inputs' signatures
::Serialize(s, CScriptBase()); ::Serialize(s, CScript());
else else
SerializeScriptCode(s); SerializeScriptCode(s);
// Serialize the nSequence // Serialize the nSequence
@ -1207,7 +1207,7 @@ uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsig
// The prevout may already be contained in hashPrevout, and the nSequence // The prevout may already be contained in hashPrevout, and the nSequence
// may already be contain in hashSequence. // may already be contain in hashSequence.
ss << txTo.vin[nIn].prevout; ss << txTo.vin[nIn].prevout;
ss << static_cast<const CScriptBase&>(scriptCode); ss << scriptCode;
ss << amount; ss << amount;
ss << txTo.vin[nIn].nSequence; ss << txTo.vin[nIn].nSequence;
// Outputs (none/one/all, depending on flags) // Outputs (none/one/all, depending on flags)

View file

@ -8,6 +8,7 @@
#include "crypto/common.h" #include "crypto/common.h"
#include "prevector.h" #include "prevector.h"
#include "serialize.h"
#include <assert.h> #include <assert.h>
#include <climits> #include <climits>
@ -404,6 +405,13 @@ public:
CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { } CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { } CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(static_cast<CScriptBase&>(*this));
}
CScript& operator+=(const CScript& b) CScript& operator+=(const CScript& b)
{ {
insert(end(), b.begin(), b.end()); insert(end(), b.begin(), b.end());

View file

@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
uint256 in2 = InsecureRand256(); uint256 in2 = InsecureRand256();
BOOST_CHECK(dbw.Write(key2, in2)); BOOST_CHECK(dbw.Write(key2, in2));
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator()); std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
// Be sure to seek past the obfuscation key (if it exists) // Be sure to seek past the obfuscation key (if it exists)
it->Seek(key); it->Seek(key);
@ -210,7 +210,7 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
BOOST_CHECK(dbw.Write(key, value)); BOOST_CHECK(dbw.Write(key, value));
} }
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator()); std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
for (int c=0; c<2; ++c) { for (int c=0; c<2; ++c) {
int seek_start; int seek_start;
if (c == 0) if (c == 0)
@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
} }
} }
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper*>(&dbw)->NewIterator()); std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
for (int c=0; c<2; ++c) { for (int c=0; c<2; ++c) {
int seek_start; int seek_start;
if (c == 0) if (c == 0)

View file

@ -172,7 +172,7 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
CCoinsViewCursor *CCoinsViewDB::Cursor() const CCoinsViewCursor *CCoinsViewDB::Cursor() const
{ {
CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper*>(&db)->NewIterator(), GetBestBlock()); CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper&>(db).NewIterator(), GetBestBlock());
/* It seems that there are no "const iterators" for LevelDB. Since we /* It seems that there are no "const iterators" for LevelDB. Since we
only need read operations on it, use a const-cast to get around only need read operations on it, use a const-cast to get around
that restriction. */ that restriction. */

View file

@ -94,23 +94,23 @@ bool CWalletDB::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
bool CWalletDB::WriteCScript(const uint160& hash, const CScript& redeemScript) bool CWalletDB::WriteCScript(const uint160& hash, const CScript& redeemScript)
{ {
return WriteIC(std::make_pair(std::string("cscript"), hash), *(const CScriptBase*)(&redeemScript), false); return WriteIC(std::make_pair(std::string("cscript"), hash), redeemScript, false);
} }
bool CWalletDB::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta) bool CWalletDB::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
{ {
if (!WriteIC(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest)), keyMeta)) { if (!WriteIC(std::make_pair(std::string("watchmeta"), dest), keyMeta)) {
return false; return false;
} }
return WriteIC(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)), '1'); return WriteIC(std::make_pair(std::string("watchs"), dest), '1');
} }
bool CWalletDB::EraseWatchOnly(const CScript &dest) bool CWalletDB::EraseWatchOnly(const CScript &dest)
{ {
if (!EraseIC(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest)))) { if (!EraseIC(std::make_pair(std::string("watchmeta"), dest))) {
return false; return false;
} }
return EraseIC(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest))); return EraseIC(std::make_pair(std::string("watchs"), dest));
} }
bool CWalletDB::WriteBestBlock(const CBlockLocator& locator) bool CWalletDB::WriteBestBlock(const CBlockLocator& locator)
@ -323,7 +323,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
{ {
wss.nWatchKeys++; wss.nWatchKeys++;
CScript script; CScript script;
ssKey >> *(CScriptBase*)(&script); ssKey >> script;
char fYes; char fYes;
ssValue >> fYes; ssValue >> fYes;
if (fYes == '1') if (fYes == '1')
@ -440,7 +440,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
else if (strType == "watchmeta") else if (strType == "watchmeta")
{ {
CScript script; CScript script;
ssKey >> *(CScriptBase*)(&script); ssKey >> script;
keyID = CScriptID(script); keyID = CScriptID(script);
} }
@ -474,7 +474,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
uint160 hash; uint160 hash;
ssKey >> hash; ssKey >> hash;
CScript script; CScript script;
ssValue >> *(CScriptBase*)(&script); ssValue >> script;
if (!pwallet->LoadCScript(script)) if (!pwallet->LoadCScript(script))
{ {
strErr = "Error reading wallet database: LoadCScript failed"; strErr = "Error reading wallet database: LoadCScript failed";