From c4ea423827206717f5ba064e0161c99574ea0f74 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 17 Jul 2020 18:31:42 +1200 Subject: [PATCH] prevector: Terminate without logging on failed allocation This reverts aeb089ecc7718fdde16304073d13a2c1082f318e, which introduced logging, adding a dependency on libbitcoin_util.a to libzcashconsensus.a. Also adds missing #includes that were being indirectly included via prevector.h including util.h. --- src/gtest/test_timedata.cpp | 1 + src/init.cpp | 14 ++++++++++++++ src/logging.cpp | 14 -------------- src/mempool_limit.cpp | 5 ++++- src/miner.h | 2 ++ src/prevector.h | 6 ++---- src/util.h | 2 -- src/zcash/Note.cpp | 4 ++++ 8 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/gtest/test_timedata.cpp b/src/gtest/test_timedata.cpp index 10a275762..4cd6135f8 100644 --- a/src/gtest/test_timedata.cpp +++ b/src/gtest/test_timedata.cpp @@ -8,6 +8,7 @@ #include "timedata.h" #include "random.h" #include "netbase.h" +#include "utiltime.h" using ::testing::StrictMock; diff --git a/src/init.cpp b/src/init.cpp index de7967995..6978c862f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -828,6 +828,20 @@ void InitLogging() LogPrintf("Zcash version %s (%s)\n", FormatFullVersion(), CLIENT_DATE); } +[[noreturn]] static void new_handler_terminate() +{ + // Rather than throwing std::bad-alloc if allocation fails, terminate + // immediately to (try to) avoid chain corruption. + // Since LogPrintf may itself allocate memory, set the handler directly + // to terminate first. + std::set_new_handler(std::terminate); + fputs("Error: Out of memory. Terminating.\n", stderr); + LogPrintf("Error: Out of memory. Terminating.\n"); + + // The log was successful, terminate now. + std::terminate(); +}; + /** Initialize bitcoin. * @pre Parameters should be parsed and config file should be read. */ diff --git a/src/logging.cpp b/src/logging.cpp index 1d9a4dad3..039f19733 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -54,20 +54,6 @@ static FILE* fileout = NULL; static boost::mutex* mutexDebugLog = NULL; static list *vMsgsBeforeOpenLog; -[[noreturn]] void new_handler_terminate() -{ - // Rather than throwing std::bad-alloc if allocation fails, terminate - // immediately to (try to) avoid chain corruption. - // Since LogPrintf may itself allocate memory, set the handler directly - // to terminate first. - std::set_new_handler(std::terminate); - fputs("Error: Out of memory. Terminating.\n", stderr); - LogPrintf("Error: Out of memory. Terminating.\n"); - - // The log was successful, terminate now. - std::terminate(); -}; - static int FileWriteStr(const std::string &str, FILE *fp) { return fwrite(str.data(), 1, str.size(), fp); diff --git a/src/mempool_limit.cpp b/src/mempool_limit.cpp index 7cd118ef2..c2137e0e3 100644 --- a/src/mempool_limit.cpp +++ b/src/mempool_limit.cpp @@ -2,11 +2,14 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or https://www.opensource.org/licenses/mit-license.php . -#include "core_memusage.h" #include "mempool_limit.h" + +#include "core_memusage.h" +#include "logging.h" #include "random.h" #include "serialize.h" #include "timedata.h" +#include "utiltime.h" #include "version.h" const TxWeight ZERO_WEIGHT = TxWeight(0, 0); diff --git a/src/miner.h b/src/miner.h index 9f3eacecf..032fce91b 100644 --- a/src/miner.h +++ b/src/miner.h @@ -12,6 +12,8 @@ #include #include +#include + class CBlockIndex; class CChainParams; class CScript; diff --git a/src/prevector.h b/src/prevector.h index 290d5f37a..e0385df79 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -5,8 +5,6 @@ #ifndef BITCOIN_PREVECTOR_H #define BITCOIN_PREVECTOR_H -#include - #include #include #include @@ -177,11 +175,11 @@ private: success. These should instead use an allocator or new/delete so that handlers are called as necessary, but performance would be slightly degraded by doing so. */ _union.indirect = static_cast(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity)); - if (!_union.indirect) { new_handler_terminate(); } + assert(_union.indirect); _union.capacity = new_capacity; } else { char* new_indirect = static_cast(malloc(((size_t)sizeof(T)) * new_capacity)); - if (!new_indirect) { new_handler_terminate(); } + assert(new_indirect); T* src = direct_ptr(0); T* dst = reinterpret_cast(new_indirect); memcpy(dst, src, size() * sizeof(T)); diff --git a/src/util.h b/src/util.h index 88b15b562..3d45b92c5 100644 --- a/src/util.h +++ b/src/util.h @@ -34,8 +34,6 @@ extern std::map > mapMultiArgs; extern bool fDebug; extern bool fServer; -[[noreturn]] extern void new_handler_terminate(); - extern const char * const BITCOIN_CONF_FILENAME; extern const char * const BITCOIN_PID_FILENAME; diff --git a/src/zcash/Note.cpp b/src/zcash/Note.cpp index e29dd91e3..b1af195ea 100644 --- a/src/zcash/Note.cpp +++ b/src/zcash/Note.cpp @@ -1,7 +1,9 @@ #include "Note.hpp" + #include "prf.h" #include "crypto/sha256.h" #include "consensus/consensus.h" +#include "logging.h" #include "random.h" #include "version.h" @@ -10,6 +12,8 @@ #include "zcash/util.h" #include "librustzcash.h" +#include + using namespace libzcash; SproutNote::SproutNote() {