Skip to content

Commit c5f008a

Browse files
committed
don't throw std::bad_alloc when out of memory. Instead, terminate immediately
1 parent f19afdb commit c5f008a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/init.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,19 @@ ServiceFlags nLocalServices = NODE_NETWORK;
797797

798798
}
799799

800+
[[noreturn]] static void new_handler_terminate()
801+
{
802+
// Rather than throwing std::bad-alloc if allocation fails, terminate
803+
// immediately to (try to) avoid chain corruption.
804+
// Since LogPrintf may itself allocate memory, set the handler directly
805+
// to terminate first.
806+
std::set_new_handler(std::terminate);
807+
LogPrintf("Error: Out of memory. Terminating.\n");
808+
809+
// The log was successful, terminate now.
810+
std::terminate();
811+
};
812+
800813
bool AppInitBasicSetup()
801814
{
802815
// ********************************************************* Step 1: setup
@@ -849,6 +862,9 @@ bool AppInitBasicSetup()
849862
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
850863
signal(SIGPIPE, SIG_IGN);
851864
#endif
865+
866+
std::set_new_handler(new_handler_terminate);
867+
852868
return true;
853869
}
854870

0 commit comments

Comments
 (0)