This is an automated email from the ASF dual-hosted git repository.

wasphin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 127362867 Fix EndPoint self-assignment (#3670)
127362867 is described below

commit 127362867565ec229a7f44d5d0820342725f884f
Author: nkysg <[email protected]>
AuthorDate: Tue Oct 27 13:50:47 2026 +0800

    Fix EndPoint self-assignment (#3670)
---
 src/butil/endpoint.cpp     |  3 +++
 test/endpoint_unittest.cpp | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/butil/endpoint.cpp b/src/butil/endpoint.cpp
index e0ed2afc5..d58dd1441 100644
--- a/src/butil/endpoint.cpp
+++ b/src/butil/endpoint.cpp
@@ -110,6 +110,9 @@ EndPoint::~EndPoint() {
 }
 
 void EndPoint::operator=(const EndPoint& rhs) {
+    if (this == &rhs) {
+        return;
+    }
     reset();
     set_endpoint(this, rhs.ip, rhs.port);
 }
diff --git a/test/endpoint_unittest.cpp b/test/endpoint_unittest.cpp
index 336faacc9..1abb06203 100644
--- a/test/endpoint_unittest.cpp
+++ b/test/endpoint_unittest.cpp
@@ -308,6 +308,38 @@ TEST(EndPointTest, original_endpoint) {
     ASSERT_EQ(ep.port, ep2.port);
 }
 
+TEST(EndPointTest, self_assignment) {
+    const char* addresses[] = {
+        "0.0.0.0:0", "1.2.3.4:5678", "[::1]:8080", "unix:sock.file"
+    };
+    for (const char* address : addresses) {
+        SCOPED_TRACE(address);
+        butil::EndPoint ep;
+        ASSERT_EQ(0, butil::str2endpoint(address, &ep));
+        const std::string before = butil::endpoint2str(ep).c_str();
+        const butil::ip_t ip = ep.ip;
+        const int port = ep.port;
+        const bool is_extended = ExtendedEndPoint::is_extended(ep);
+        ExtendedEndPoint* eep = nullptr;
+        int64_t ref_count = 0;
+        if (is_extended) {
+            eep = ExtendedEndPoint::address(ep);
+            ref_count = eep->_ref_count.load();
+        }
+        const butil::EndPoint& alias = ep;
+        ep = alias;
+        ASSERT_EQ(before, butil::endpoint2str(ep).c_str());
+        ASSERT_EQ(is_extended, ExtendedEndPoint::is_extended(ep));
+        if (is_extended) {
+            ASSERT_EQ(eep, ExtendedEndPoint::address(ep));
+            ASSERT_EQ(ref_count, eep->_ref_count.load());
+        } else {
+            ASSERT_EQ(ip, ep.ip);
+            ASSERT_EQ(port, ep.port);
+        }
+    }
+}
+
 TEST(EndPointTest, extended_endpoint) {
     butil::EndPoint ep;
     ASSERT_EQ(0, butil::str2endpoint("unix:sock.file", &ep));


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to