You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by ww...@apache.org on 2023/02/06 01:57:47 UTC

[brpc] branch master updated: check invalid port

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

wwbmmm 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 e75ff7d8 check invalid port
     new 14ff36a2 Merge pull request #2108 from chenBright/invalid_port
e75ff7d8 is described below

commit e75ff7d8e4348590d644af2a6ddd679b826818bb
Author: ChenBright <ch...@foxmail.com>
AuthorDate: Sat Feb 4 15:22:13 2023 +0800

    check invalid port
---
 src/brpc/server.cpp           | 12 ++++++++----
 test/brpc_server_unittest.cpp | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp
index a7db3193..bf4227c3 100644
--- a/src/brpc/server.cpp
+++ b/src/brpc/server.cpp
@@ -998,6 +998,10 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
     }
     _listen_addr = endpoint;
     for (int port = port_range.min_port; port <= port_range.max_port; ++port) {
+        if (port < 0 || port > 65535) {
+            LOG(ERROR) << "Invalid port=" << port;
+            return -1;
+        }
         _listen_addr.port = port;
         butil::fd_guard sockfd(tcp_listen(_listen_addr));
         if (sockfd < 0) {
@@ -1047,6 +1051,10 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
         break; // stop trying
     }
     if (_options.internal_port >= 0 && _options.has_builtin_services) {
+        if (_options.internal_port < 0 || _options.internal_port > 65535) {
+            LOG(ERROR) << "Invalid internal port=" << _options.internal_port;
+            return -1;
+        }
         if (_options.internal_port  == _listen_addr.port) {
             LOG(ERROR) << "ServerOptions.internal_port=" << _options.internal_port
                        << " is same with port=" << _listen_addr.port << " to Start()";
@@ -1143,10 +1151,6 @@ int Server::Start(const char* ip_port_str, const ServerOptions* opt) {
 }
 
 int Server::Start(int port, const ServerOptions* opt) {
-    if (port < 0 || port > 65535) {
-        LOG(ERROR) << "Invalid port=" << port;
-        return -1;
-    }
     return Start(butil::EndPoint(butil::IP_ANY, port), opt);
 }
 
diff --git a/test/brpc_server_unittest.cpp b/test/brpc_server_unittest.cpp
index c22b6b53..ce5e0448 100644
--- a/test/brpc_server_unittest.cpp
+++ b/test/brpc_server_unittest.cpp
@@ -206,12 +206,32 @@ TEST_F(ServerTest, sanity) {
         ASSERT_EQ(-1, server.Start(99999, NULL));
         ASSERT_EQ(0, server.Start(8613, NULL));
     }
+
+    {
+        brpc::Server server1;
+        brpc::PortRange range1(65534, 65535);
+        ASSERT_EQ(0, server1.Start(range1, NULL));
+
+        brpc::Server server2;
+        ASSERT_EQ(0, server2.Start(range1, NULL));
+
+        brpc::Server server3;
+        ASSERT_EQ(-1, server3.Start(range1, NULL));
+
+        brpc::Server server4;
+        brpc::PortRange range4(65535, 65536);
+        ASSERT_EQ(-1, server4.Start(range4, NULL));
+    }
+
     {
         brpc::Server server;
         brpc::ServerOptions options;
         options.internal_port = 8613;          // The same as service port
         ASSERT_EQ(-1, server.Start("127.0.0.1:8613", &options));
         ASSERT_FALSE(server.IsRunning());      // Revert server's status
+        options.internal_port = 65536;          // Invalid port
+        ASSERT_EQ(-1, server.Start("127.0.0.1:8613", &options));
+        ASSERT_FALSE(server.IsRunning());      // Revert server's status
         // And release the listen port
         ASSERT_EQ(0, server.Start("127.0.0.1:8613", NULL));
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org