changeset 26730:1a7b1b9d148b

don't throw std::bad_alloc when out of memory. Instead, terminate immediately
author Cory Fields <cory-nospam-@coryfields.com>
date Fri, 24 Feb 2017 18:20:03 -0500
parents a8520fefcedf
children 05e9c7f7436f
files src/init.cpp
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/init.cpp	Fri Feb 24 12:48:45 2017 +0100
+++ b/src/init.cpp	Fri Feb 24 18:20:03 2017 -0500
@@ -797,6 +797,19 @@
 
 }
 
+[[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 @@
     // 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;
 }