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

Commit

Permalink
Merge pull request #8307 from EOSIO/fix/reset-new-handler
Browse files Browse the repository at this point in the history
reset the new handler
  • Loading branch information
larryk85 authored Dec 11, 2019
2 parents a120c27 + 4599d37 commit 3bd3713
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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 @@ -213,6 +215,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;
chainbase::database db;
chainbase::database reversible_blocks; ///< a special database to persist blocks that have successfully been applied but are still reversible
Expand Down Expand Up @@ -286,7 +296,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 @@ -57,7 +57,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

0 comments on commit 3bd3713

Please sign in to comment.