Skip to content

Commit

Permalink
Fix uninitialized local side of Socket
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Jun 26, 2024
1 parent 2e18318 commit 7132979
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/brpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ int Socket::OnCreated(const SocketOptions& options) {
_keytable_pool = options.keytable_pool;
_tos = 0;
_remote_side = options.remote_side;
_local_side = butil::EndPoint();
_on_edge_triggered_events = options.on_edge_triggered_events;
_user = options.user;
_conn = options.conn;
Expand Down
11 changes: 11 additions & 0 deletions test/brpc_socket_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ TEST_F(SocketTest, single_threaded_connect_and_write) {
global_sock = s.get();
ASSERT_TRUE(s.get());
ASSERT_EQ(-1, s->fd());
ASSERT_EQ(butil::EndPoint(), s->local_side());
ASSERT_EQ(point, s->remote_side());
ASSERT_EQ(id, s->id());
for (size_t i = 0; i < 20; ++i) {
Expand Down Expand Up @@ -415,6 +416,16 @@ TEST_F(SocketTest, single_threaded_connect_and_write) {
ASSERT_EQ(-1, messenger->listened_fd());
ASSERT_EQ(-1, fcntl(listening_fd, F_GETFD));
ASSERT_EQ(EBADF, errno);

// The socket object is likely to be reused,
// and the local side should be initialized.
ASSERT_EQ(0, brpc::Socket::Create(options, &id));
brpc::SocketUniquePtr s;
ASSERT_EQ(0, brpc::Socket::Address(id, &s));
ASSERT_TRUE(s.get());
ASSERT_EQ(-1, s->fd());
ASSERT_EQ(butil::EndPoint(), s->local_side());
ASSERT_EQ(point, s->remote_side());
}

#define NUMBER_WIDTH 16
Expand Down

0 comments on commit 7132979

Please sign in to comment.