Skip to content

Commit 5c7c3b3

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#9856: Terminate immediately when allocation fails
d4ee7ba prevector: assert successful allocation (Cory Fields) c5f008a don't throw std::bad_alloc when out of memory. Instead, terminate immediately (Cory Fields) Tree-SHA512: 699ce8df5b1775a99c71d3cfc952b45da1c0091e1a4b6adfac52d5be6144c3d98f88ac3af90e5c73fff2f74666a499feb4a34434683ce5979814e869c0aeddc3
1 parent 397c27f commit 5c7c3b3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/init.cpp

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

942942
}
943943

944+
[[noreturn]] static void new_handler_terminate()
945+
{
946+
// Rather than throwing std::bad-alloc if allocation fails, terminate
947+
// immediately to (try to) avoid chain corruption.
948+
// Since LogPrintf may itself allocate memory, set the handler directly
949+
// to terminate first.
950+
std::set_new_handler(std::terminate);
951+
LogPrintf("Error: Out of memory. Terminating.\n");
952+
953+
// The log was successful, terminate now.
954+
std::terminate();
955+
};
956+
944957
bool AppInitBasicSetup()
945958
{
946959
// ********************************************************* Step 1: setup
@@ -993,6 +1006,9 @@ bool AppInitBasicSetup()
9931006
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
9941007
signal(SIGPIPE, SIG_IGN);
9951008
#endif
1009+
1010+
std::set_new_handler(new_handler_terminate);
1011+
9961012
return true;
9971013
}
9981014

src/prevector.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef _BITCOIN_PREVECTOR_H_
66
#define _BITCOIN_PREVECTOR_H_
77

8+
#include <assert.h>
89
#include <stdlib.h>
910
#include <stdint.h>
1011
#include <string.h>
@@ -170,10 +171,15 @@ class prevector {
170171
}
171172
} else {
172173
if (!is_direct()) {
174+
/* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
175+
success. These should instead use an allocator or new/delete so that handlers
176+
are called as necessary, but performance would be slightly degraded by doing so. */
173177
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
178+
assert(_union.indirect);
174179
_union.capacity = new_capacity;
175180
} else {
176181
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
182+
assert(new_indirect);
177183
T* src = direct_ptr(0);
178184
T* dst = reinterpret_cast<T*>(new_indirect);
179185
memcpy(dst, src, size() * sizeof(T));

0 commit comments

Comments
 (0)