prevector: Terminate without logging on failed allocation

This reverts aeb089ecc7, 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.
This commit is contained in:
Jack Grigg 2020-07-17 18:31:42 +12:00
parent 79ad5984b1
commit c4ea423827
8 changed files with 27 additions and 21 deletions

View File

@ -8,6 +8,7 @@
#include "timedata.h" #include "timedata.h"
#include "random.h" #include "random.h"
#include "netbase.h" #include "netbase.h"
#include "utiltime.h"
using ::testing::StrictMock; using ::testing::StrictMock;

View File

@ -828,6 +828,20 @@ void InitLogging()
LogPrintf("Zcash version %s (%s)\n", FormatFullVersion(), CLIENT_DATE); 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. /** Initialize bitcoin.
* @pre Parameters should be parsed and config file should be read. * @pre Parameters should be parsed and config file should be read.
*/ */

View File

@ -54,20 +54,6 @@ static FILE* fileout = NULL;
static boost::mutex* mutexDebugLog = NULL; static boost::mutex* mutexDebugLog = NULL;
static list<string> *vMsgsBeforeOpenLog; static list<string> *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) static int FileWriteStr(const std::string &str, FILE *fp)
{ {
return fwrite(str.data(), 1, str.size(), fp); return fwrite(str.data(), 1, str.size(), fp);

View File

@ -2,11 +2,14 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php . // file COPYING or https://www.opensource.org/licenses/mit-license.php .
#include "core_memusage.h"
#include "mempool_limit.h" #include "mempool_limit.h"
#include "core_memusage.h"
#include "logging.h"
#include "random.h" #include "random.h"
#include "serialize.h" #include "serialize.h"
#include "timedata.h" #include "timedata.h"
#include "utiltime.h"
#include "version.h" #include "version.h"
const TxWeight ZERO_WEIGHT = TxWeight(0, 0); const TxWeight ZERO_WEIGHT = TxWeight(0, 0);

View File

@ -12,6 +12,8 @@
#include <stdint.h> #include <stdint.h>
#include <variant> #include <variant>
#include <boost/shared_ptr.hpp>
class CBlockIndex; class CBlockIndex;
class CChainParams; class CChainParams;
class CScript; class CScript;

View File

@ -5,8 +5,6 @@
#ifndef BITCOIN_PREVECTOR_H #ifndef BITCOIN_PREVECTOR_H
#define BITCOIN_PREVECTOR_H #define BITCOIN_PREVECTOR_H
#include <util.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -177,11 +175,11 @@ private:
success. These should instead use an allocator or new/delete so that handlers 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. */ are called as necessary, but performance would be slightly degraded by doing so. */
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity)); _union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
if (!_union.indirect) { new_handler_terminate(); } assert(_union.indirect);
_union.capacity = new_capacity; _union.capacity = new_capacity;
} else { } else {
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity)); char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
if (!new_indirect) { new_handler_terminate(); } assert(new_indirect);
T* src = direct_ptr(0); T* src = direct_ptr(0);
T* dst = reinterpret_cast<T*>(new_indirect); T* dst = reinterpret_cast<T*>(new_indirect);
memcpy(dst, src, size() * sizeof(T)); memcpy(dst, src, size() * sizeof(T));

View File

@ -34,8 +34,6 @@ extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
extern bool fDebug; extern bool fDebug;
extern bool fServer; extern bool fServer;
[[noreturn]] extern void new_handler_terminate();
extern const char * const BITCOIN_CONF_FILENAME; extern const char * const BITCOIN_CONF_FILENAME;
extern const char * const BITCOIN_PID_FILENAME; extern const char * const BITCOIN_PID_FILENAME;

View File

@ -1,7 +1,9 @@
#include "Note.hpp" #include "Note.hpp"
#include "prf.h" #include "prf.h"
#include "crypto/sha256.h" #include "crypto/sha256.h"
#include "consensus/consensus.h" #include "consensus/consensus.h"
#include "logging.h"
#include "random.h" #include "random.h"
#include "version.h" #include "version.h"
@ -10,6 +12,8 @@
#include "zcash/util.h" #include "zcash/util.h"
#include "librustzcash.h" #include "librustzcash.h"
#include <boost/thread/exceptions.hpp>
using namespace libzcash; using namespace libzcash;
SproutNote::SproutNote() { SproutNote::SproutNote() {