Attempt to log before terminating if prevector allocation fails

This commit is contained in:
Jack Grigg
2018-04-26 11:30:33 +01:00
parent 3c9dbf3ed8
commit aeb089ecc7
4 changed files with 21 additions and 16 deletions

View File

@@ -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);