Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

reset the new handler (develop) #8313

Merged
merged 4 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <fc/scoped_exit.hpp>
#include <fc/variant_object.hpp>

#include <new>

namespace eosio { namespace chain {

using resource_limits::resource_limits_manager;
Expand Down Expand Up @@ -214,6 +216,14 @@ struct pending_state {
};

struct controller_impl {

// LLVM sets the new handler, we need to reset this to throw a bad_alloc exception so we can possibly exit cleanly
// and not just abort.
struct reset_new_handler {
reset_new_handler() { std::set_new_handler([](){ throw std::bad_alloc(); }); }
};

reset_new_handler rnh; // placed here to allow for this to be set before constructing the other fields
controller& self;
std::function<void()> shutdown;
chainbase::database db;
Expand Down Expand Up @@ -288,7 +298,8 @@ struct controller_impl {
}

controller_impl( const controller::config& cfg, controller& s, protocol_feature_set&& pfs, const chain_id_type& chain_id )
:self(s),
:rnh(),
self(s),
db( cfg.state_dir,
cfg.read_only ? database::read_only : database::read_write,
cfg.state_size, false, cfg.db_map_mode, cfg.db_hugepage_paths ),
Expand Down
1 change: 0 additions & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ namespace eosio { namespace chain {

class controller {
public:

struct config {
flat_set<account_name> sender_bypass_whiteblacklist;
flat_set<account_name> actor_whitelist;
Expand Down
10 changes: 10 additions & 0 deletions unittests/misc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,16 @@ BOOST_AUTO_TEST_CASE(stable_priority_queue_test) {
} FC_LOG_AND_RETHROW()
}

// test that std::bad_alloc is being thrown
BOOST_AUTO_TEST_CASE(bad_alloc_test) {
tester t; // force a controller to be constructed and set the new_handler
int* ptr = nullptr;
const auto fail = [&]() {
ptr = new int[std::numeric_limits<int64_t>::max()/16];
};
BOOST_CHECK_THROW( fail(), std::bad_alloc );
BOOST_CHECK( ptr == nullptr );
}

BOOST_AUTO_TEST_SUITE_END()

Expand Down