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 #831 from EOSIO/feature/stat-167-gh-704-read-buffe…
Browse files Browse the repository at this point in the history
…r-safety

stat 167 gh 704 read buffer safety
  • Loading branch information
heifner authored Nov 30, 2017
2 parents 42edb15 + f8dc774 commit de42c5c
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ namespace eosio {
out_queue.clear();
if(response_expected) {
response_expected->cancel();
response_expected.reset();
}
pending_message_buffer.reset();
}
Expand Down Expand Up @@ -867,7 +868,7 @@ namespace eosio {
} else {
if(conn->out_queue.size()) {
if(conn->out_queue.front().contains<go_away_message>()) {
conn->close();
my_impl->close(conn);
return;
}
conn->out_queue.pop_front();
Expand Down Expand Up @@ -1252,7 +1253,7 @@ namespace eosio {
elog( "connection failed to ${peer}: ${error}",
( "peer", c->peer_name())("error",err.message()));
c->connecting = false;
c->close();
my_impl->close(c);
}
}
} );
Expand Down Expand Up @@ -1295,17 +1296,11 @@ namespace eosio {
void net_plugin_impl::start_read_message( connection_ptr conn ) {

try {
connection_wptr c( conn);
conn->socket->async_read_some(
conn->pending_message_buffer.get_buffer_sequence_for_boost_async_read(),
[this,c]( boost::system::error_code ec, std::size_t bytes_transferred ) {
[this,conn]( boost::system::error_code ec, std::size_t bytes_transferred ) {
try {
if( !ec ) {
connection_ptr conn = c.lock();
if (!conn) {
return;
}

FC_ASSERT(bytes_transferred <= conn->pending_message_buffer.bytes_to_write());
conn->pending_message_buffer.advance_write_ptr(bytes_transferred);
while (conn->pending_message_buffer.bytes_to_read() > 0) {
Expand All @@ -1330,15 +1325,15 @@ namespace eosio {
start_read_message(conn);
} else {
elog( "Error reading message from connection: ${m}",( "m", ec.message() ) );
close( c.lock() );
close( conn );
}
} catch (...) {
close(c.lock());
close( conn );
}
}
);
} catch (...) {
close(conn);
close( conn );
}
}

Expand Down Expand Up @@ -1976,7 +1971,7 @@ namespace eosio {
if( discards.size( ) ) {
for( auto &c : discards) {
connections.erase( c );
c.reset( );
c.reset();
}
}
}
Expand Down Expand Up @@ -2438,7 +2433,6 @@ namespace eosio {
ilog( "close ${s} connections",( "s",my->connections.size()) );
auto cons = my->connections;
for( auto con : cons ) {
con->socket->close();
my->close( con);
}

Expand Down Expand Up @@ -2473,6 +2467,8 @@ namespace eosio {
string net_plugin::disconnect( const string& host ) {
for( auto itr = my->connections.begin(); itr != my->connections.end(); ++itr ) {
if( (*itr)->peer_addr == host ) {
(*itr)->reset();
my->close(*itr);
my->connections.erase(itr);
return "connection removed";
}
Expand Down

0 comments on commit de42c5c

Please sign in to comment.