Browse Source

Merge #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
0.15
Wladimir J. van der Laan 8 years ago
parent
commit
65fdc37ac3
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 16
      src/init.cpp
  2. 6
      src/prevector.h

16
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() bool AppInitBasicSetup()
{ {
// ********************************************************* Step 1: setup // ********************************************************* Step 1: setup
@ -849,6 +862,9 @@ bool AppInitBasicSetup()
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly // Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif #endif
std::set_new_handler(new_handler_terminate);
return true; return true;
} }

6
src/prevector.h

@ -5,6 +5,7 @@
#ifndef _BITCOIN_PREVECTOR_H_ #ifndef _BITCOIN_PREVECTOR_H_
#define _BITCOIN_PREVECTOR_H_ #define _BITCOIN_PREVECTOR_H_
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@ -170,10 +171,15 @@ private:
} }
} else { } else {
if (!is_direct()) { if (!is_direct()) {
/* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
success. These should instead use an allocator or new/delete so that handlers
are called as necessary, but performance would be slightly degraded by doing so. */
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity)); _union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
assert(_union.indirect);
_union.capacity = new_capacity; _union.capacity = new_capacity;
} else { } else {
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity)); char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
assert(new_indirect);
T* src = direct_ptr(0); T* src = direct_ptr(0);
T* dst = reinterpret_cast<T*>(new_indirect); T* dst = reinterpret_cast<T*>(new_indirect);
memcpy(dst, src, size() * sizeof(T)); memcpy(dst, src, size() * sizeof(T));

Loading…
Cancel
Save