Attempt to log before terminating if prevector allocation fails
This commit is contained in:
15
src/util.cpp
15
src/util.cpp
@@ -17,6 +17,7 @@
|
||||
#include "utiltime.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
|
||||
#include <pthread.h>
|
||||
@@ -179,6 +180,20 @@ static FILE* fileout = NULL;
|
||||
static boost::mutex* mutexDebugLog = NULL;
|
||||
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)
|
||||
{
|
||||
return fwrite(str.data(), 1, str.size(), fp);
|
||||
|
||||
Reference in New Issue
Block a user