Skip to content

Commit

Permalink
fix: tcp recv buffer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
kibae committed Sep 17, 2023
1 parent 6969965 commit 3b2e85a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/transport/tcp/tcp_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define NTOHLL(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
#endif

#define MAX_RECV_BUF_LENGTH 1024 * 1024 * 4

namespace onnxruntime_server::transport::tcp {
struct protocol_header {
int16_t type;
Expand All @@ -29,7 +31,7 @@ namespace onnxruntime_server::transport::tcp {
private:
asio::socket socket;
tcp_server *server;
char chunk[1024];
char chunk[MAX_RECV_BUF_LENGTH];
std::string buffer;

onnxruntime_server::task::benchmark request_time;
Expand Down
16 changes: 1 addition & 15 deletions src/transport/tcp/tcp_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ void onnxruntime_server::transport::tcp::tcp_session::close() {
server->remove_session(shared_from_this());
}

#define MAX_LENGTH 1024 * 16

void Orts::transport::tcp::tcp_session::do_read() {
if (_remote_endpoint.empty())
_remote_endpoint =
socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port());

socket.async_read_some(
boost::asio::buffer(chunk, MAX_LENGTH),
boost::asio::buffer(chunk, MAX_RECV_BUF_LENGTH),
[self = shared_from_this()](boost::system::error_code ec, std::size_t length) {
if (!ec) {
self->buffer.append(self->chunk, length);
Expand All @@ -42,18 +40,6 @@ void Orts::transport::tcp::tcp_session::do_read() {
header.length = NTOHLL(header.length);
header.json_length = NTOHLL(header.json_length);
header.post_length = NTOHLL(header.post_length);
// assert(header.length == header.json_length + header.post_length);

/*
// check buffer size
if (header.length > MAX_BUFFER_LIMIT) {
PLOG(L_WARNING) << self->get_remote_endpoint()
<< " transport::session::do_read: Buffer size is too large: " << header.length
<< std::endl;
self->close();
return;
}
*/

// continue to read
if (self->buffer.size() < sizeof(protocol_header) + header.length) {
Expand Down

0 comments on commit 3b2e85a

Please sign in to comment.