don't throw std::bad_alloc when out of memory. Instead, terminate immediately

Github-Pull: #9856
Rebased-From: c5f008a416
This commit is contained in:
Cory Fields 2017-02-24 18:20:03 -05:00 committed by Wladimir J. van der Laan
parent 50953c2aad
commit 69832aaad5
No known key found for this signature in database
GPG key ID: 74810B012346C9A6

View file

@ -801,6 +801,19 @@ ServiceFlags nLocalServices = NODE_NETWORK;
}
[[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);
LogPrintf("Error: Out of memory. Terminating.\n");
// The log was successful, terminate now.
std::terminate();
};
bool AppInitBasicSetup()
{
// ********************************************************* Step 1: setup
@ -853,6 +866,9 @@ bool AppInitBasicSetup()
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
signal(SIGPIPE, SIG_IGN);
#endif
std::set_new_handler(new_handler_terminate);
return true;
}