aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCory Fields <cory-nospam-@coryfields.com>2017-02-24 18:20:03 -0500
committerWladimir J. van der Laan <laanwj@gmail.com>2017-02-28 11:40:50 +0100
commit69832aaad53c9236062ad89c28ad5f28b359b448 (patch)
tree2f9fa6f36ad07b17fde074bad71310208f62e889 /src
parent50953c2aadca85c8664eac612072b0df98fceee7 (diff)
downloadbitcoin-69832aaad53c9236062ad89c28ad5f28b359b448.tar.xz
don't throw std::bad_alloc when out of memory. Instead, terminate immediately
Github-Pull: #9856 Rebased-From: c5f008a4166bae4350881a74fc04a87d7a5c4ed5
Diffstat (limited to 'src')
-rw-r--r--src/init.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 7c108ac4a6..5820d8f57c 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -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;
}