Skip to content

Commit

Permalink
Merge pull request libbitcoin#412 from evoskuil/master
Browse files Browse the repository at this point in the history
Fix read buffer overallocation, use capacity not size.
  • Loading branch information
evoskuil authored May 29, 2024
2 parents 0484c5e + abc445c commit f1e2594
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/net/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void proxy::handle_read_heading(const code& ec, size_t) NOEXCEPT
return;
}

// Buffer reserve increases with each larger message (up to maximum).
// Buffer capacity increases with each larger message (up to maximum).
payload_buffer_.resize(head->payload_size);

// Post handle_read_payload to strand upon stop, error, or buffer full.
Expand Down Expand Up @@ -320,11 +320,14 @@ void proxy::handle_read_payload(const code& ec, size_t LOG_ONLY(payload_size),
}

// Don't retain larger than configured (time-space tradeoff).
payload_buffer_.resize(std::min(payload_buffer_.size(), minimum_buffer()));
payload_buffer_.shrink_to_fit();
if (minimum_buffer() < payload_buffer_.capacity())
{
payload_buffer_.resize(minimum_buffer());
payload_buffer_.shrink_to_fit();
}

LOGX("Recv " << head->command << " from [" << authority()
<< "] (" << payload_size << " bytes)");
LOGX("Recv " << head->command << " from [" << authority() << "] ("
<< payload_size << " bytes)");

signal_activity();
read_heading();
Expand Down

0 comments on commit f1e2594

Please sign in to comment.