aboutsummaryrefslogtreecommitdiff
path: root/src/init.cpp
diff options
context:
space:
mode:
authorCory Fields <[email protected]>2017-02-24 18:20:03 -0500
committerCory Fields <[email protected]>2017-02-25 01:09:11 -0500
commitc5f008a4166bae4350881a74fc04a87d7a5c4ed5 (patch)
treee53e2b702f66a79321fcd9f9f9a2f419ccde2387 /src/init.cpp
parentMerge #9612: [trivial] Rephrase the definition of difficulty. (diff)
downloaddiscoin-c5f008a4166bae4350881a74fc04a87d7a5c4ed5.tar.xz
discoin-c5f008a4166bae4350881a74fc04a87d7a5c4ed5.zip
don't throw std::bad_alloc when out of memory. Instead, terminate immediately
Diffstat (limited to 'src/init.cpp')
-rw-r--r--src/init.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 196b840cb..22c8974a5 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -797,6 +797,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
@@ -849,6 +862,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;
}