[Lldb-commits] [lldb] [lldb] Fix lldb-server host and port address parsing (PR #191414)

Georgiy Samoylov via lldb-commits lldb-commits at lists.llvm.org
Mon Apr 13 01:55:24 PDT 2026


https://github.com/sga-sc updated https://github.com/llvm/llvm-project/pull/191414

>From d71a9d363c966765014c350c674ff530b0284a74 Mon Sep 17 00:00:00 2001
From: Georgiy Samoylov <Ignitor21838 at gmail.com>
Date: Fri, 10 Apr 2026 16:06:13 +0300
Subject: [PATCH 1/2] [lldb] Fix host and port address parsing

---
 lldb/source/Host/common/Socket.cpp | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp
index 041dfbda0c21d..134591ebf9929 100644
--- a/lldb/source/Host/common/Socket.cpp
+++ b/lldb/source/Host/common/Socket.cpp
@@ -290,7 +290,12 @@ Socket::UdpConnect(llvm::StringRef host_and_port) {
 
 llvm::Expected<Socket::HostAndPort>
 Socket::DecodeHostAndPort(llvm::StringRef host_and_port) {
-  static llvm::Regex g_regex("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
+  // This regex parses host:port combinations, supporting:
+  // - IPv4 sockets (e.g., "127.0.0.1:8080")
+  // - IPv6 sockets with host part in square brackets (e.g., "[::1]:80")
+  // Group 1: Address (IPv4, hostname, or IPv6 in [])
+  // Group 2: Port number (digits only)
+  static llvm::Regex g_regex("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+$)");
   HostAndPort ret;
   llvm::SmallVector<llvm::StringRef, 3> matches;
   if (g_regex.match(host_and_port, &matches)) {
@@ -300,16 +305,13 @@ Socket::DecodeHostAndPort(llvm::StringRef host_and_port) {
       ret.hostname = ret.hostname.substr(1, ret.hostname.size() - 2);
     if (to_integer(matches[2], ret.port, 10))
       return ret;
-  } else {
-    // If this was unsuccessful, then check if it's simply an unsigned 16-bit
-    // integer, representing a port with an empty host.
-    if (to_integer(host_and_port, ret.port, 10))
-      return ret;
   }
 
-  return llvm::createStringError(llvm::inconvertibleErrorCode(),
-                                 "invalid host:port specification: '%s'",
-                                 host_and_port.str().c_str());
+  return llvm::createStringError(
+      llvm::inconvertibleErrorCode(),
+      "invalid host:port specification: '%s', both IPv4 (e.g., localhost:8080) "
+      "or IPv6 (e.g, [2001:db8::1]:8080) formats are supported",
+      host_and_port.str().c_str());
 }
 
 IOObject::WaitableHandle Socket::GetWaitableHandle() {

>From 47ca7f86e89abffe111550e1accb521a31edcb23 Mon Sep 17 00:00:00 2001
From: Georgiy Samoylov <Ignitor21838 at gmail.com>
Date: Mon, 13 Apr 2026 11:53:51 +0300
Subject: [PATCH 2/2] Adapt unit-tests

---
 lldb/unittests/Host/SocketTest.cpp                       | 9 +++------
 .../gdb-remote/GDBRemoteCommunicationServerLLGSTest.cpp  | 4 ++--
 2 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/lldb/unittests/Host/SocketTest.cpp b/lldb/unittests/Host/SocketTest.cpp
index 4ce436ec0214d..1c1a8e7093bf8 100644
--- a/lldb/unittests/Host/SocketTest.cpp
+++ b/lldb/unittests/Host/SocketTest.cpp
@@ -45,20 +45,17 @@ TEST_F(SocketTest, DecodeHostAndPort) {
   EXPECT_THAT_EXPECTED(
       Socket::DecodeHostAndPort("google.com:65536"),
       llvm::FailedWithMessage(
-          "invalid host:port specification: 'google.com:65536'"));
+          "invalid host:port specification: 'google.com:65536', both IPv4 (e.g., localhost:8080) or IPv6 (e.g, [2001:db8::1]:8080) formats are supported"));
 
   EXPECT_THAT_EXPECTED(
       Socket::DecodeHostAndPort("google.com:-1138"),
       llvm::FailedWithMessage(
-          "invalid host:port specification: 'google.com:-1138'"));
+          "invalid host:port specification: 'google.com:-1138', both IPv4 (e.g., localhost:8080) or IPv6 (e.g, [2001:db8::1]:8080) formats are supported"));
 
   EXPECT_THAT_EXPECTED(
       Socket::DecodeHostAndPort("google.com:65536"),
       llvm::FailedWithMessage(
-          "invalid host:port specification: 'google.com:65536'"));
-
-  EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("12345"),
-                       llvm::HasValue(Socket::HostAndPort{"", 12345}));
+          "invalid host:port specification: 'google.com:65536', both IPv4 (e.g., localhost:8080) or IPv6 (e.g, [2001:db8::1]:8080) formats are supported"));
 
   EXPECT_THAT_EXPECTED(Socket::DecodeHostAndPort("*:0"),
                        llvm::HasValue(Socket::HostAndPort{"*", 0}));
diff --git a/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationServerLLGSTest.cpp b/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationServerLLGSTest.cpp
index 23ab9a1b09dca..b4704606e8692 100644
--- a/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationServerLLGSTest.cpp
+++ b/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationServerLLGSTest.cpp
@@ -34,7 +34,6 @@ TEST(GDBRemoteCommunicationServerLLGSTest, LLGSArgToURL) {
   // LLGS listen host:port pairs should be converted to listen://
   EXPECT_EQ(LLGSArgToURL("127.0.0.1:1234", false), "listen://127.0.0.1:1234");
   EXPECT_EQ(LLGSArgToURL("[::1]:1234", false), "listen://[::1]:1234");
-  EXPECT_EQ(LLGSArgToURL("[[::1]:1234]", false), "listen://[[::1]:1234]");
   EXPECT_EQ(LLGSArgToURL("localhost:1234", false), "listen://localhost:1234");
   EXPECT_EQ(LLGSArgToURL("*:1234", false), "listen://*:1234");
 
@@ -45,19 +44,20 @@ TEST(GDBRemoteCommunicationServerLLGSTest, LLGSArgToURL) {
   EXPECT_EQ(LLGSArgToURL("/tmp/foo", false), "unix-accept:///tmp/foo");
   EXPECT_EQ(LLGSArgToURL("127.0.0.1", false), "unix-accept://127.0.0.1");
   EXPECT_EQ(LLGSArgToURL("[::1]", false), "unix-accept://[::1]");
+  EXPECT_EQ(LLGSArgToURL("[[::1]:1234]", false), "unix-accept://[[::1]:1234]");
   EXPECT_EQ(LLGSArgToURL("localhost", false), "unix-accept://localhost");
   EXPECT_EQ(LLGSArgToURL(":frobnicate", false), "unix-accept://:frobnicate");
 
   // LLGS reverse connect host:port pairs should be converted to connect://
   EXPECT_EQ(LLGSArgToURL("127.0.0.1:1234", true), "connect://127.0.0.1:1234");
   EXPECT_EQ(LLGSArgToURL("[::1]:1234", true), "connect://[::1]:1234");
-  EXPECT_EQ(LLGSArgToURL("[[::1]:1234]", true), "connect://[[::1]:1234]");
   EXPECT_EQ(LLGSArgToURL("localhost:1234", true), "connect://localhost:1234");
 
   // with LLGS reverse connect, anything else goes as unix-connect://
   EXPECT_EQ(LLGSArgToURL("/tmp/foo", true), "unix-connect:///tmp/foo");
   EXPECT_EQ(LLGSArgToURL("127.0.0.1", true), "unix-connect://127.0.0.1");
   EXPECT_EQ(LLGSArgToURL("[::1]", true), "unix-connect://[::1]");
+  EXPECT_EQ(LLGSArgToURL("[[::1]:1234]", true), "unix-connect://[[::1]:1234]");
   EXPECT_EQ(LLGSArgToURL("localhost", true), "unix-connect://localhost");
   EXPECT_EQ(LLGSArgToURL(":frobnicate", true), "unix-connect://:frobnicate");
 }



More information about the lldb-commits mailing list