File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -797,6 +797,19 @@ ServiceFlags nLocalServices = NODE_NETWORK;
797
797
798
798
}
799
799
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
+
800
813
bool AppInitBasicSetup ()
801
814
{
802
815
// ********************************************************* Step 1: setup
@@ -849,6 +862,9 @@ bool AppInitBasicSetup()
849
862
// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
850
863
signal (SIGPIPE, SIG_IGN);
851
864
#endif
865
+
866
+ std::set_new_handler (new_handler_terminate);
867
+
852
868
return true ;
853
869
}
854
870
Original file line number Diff line number Diff line change 5
5
#ifndef _BITCOIN_PREVECTOR_H_
6
6
#define _BITCOIN_PREVECTOR_H_
7
7
8
+ #include < assert.h>
8
9
#include < stdlib.h>
9
10
#include < stdint.h>
10
11
#include < string.h>
@@ -170,10 +171,15 @@ class prevector {
170
171
}
171
172
} else {
172
173
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. */
173
177
_union.indirect = static_cast <char *>(realloc (_union.indirect , ((size_t )sizeof (T)) * new_capacity));
178
+ assert (_union.indirect );
174
179
_union.capacity = new_capacity;
175
180
} else {
176
181
char * new_indirect = static_cast <char *>(malloc (((size_t )sizeof (T)) * new_capacity));
182
+ assert (new_indirect);
177
183
T* src = direct_ptr (0 );
178
184
T* dst = reinterpret_cast <T*>(new_indirect);
179
185
memcpy (dst, src, size () * sizeof (T));
You can’t perform that action at this time.
0 commit comments