Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uninitialized local side of Socket #2672

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/brpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,7 @@ int Socket::ResetFileDescriptor(int fd) {
butil::get_remote_side(fd, &_remote_side);
}
// OK to fail, non-socket fd does not support this.
if (butil::get_local_side(fd, &_local_side) != 0) {
_local_side = butil::EndPoint();
}
butil::get_local_side(fd, &_local_side);

// FIXME : close-on-exec should be set by new syscalls or worse: set right
// after fd-creation syscall. Setting at here has higher probabilities of
Expand Down Expand Up @@ -720,6 +718,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
Loading