mgorny created this revision.
mgorny added reviewers: labath, krytarowski, emaste, JDevlieghere.
mgorny requested review of this revision.
Refactor Socket::DecodeHostAndPort() to use LLVM API over redundant
LLDB API. In particular, this means llvm::Regex and
llvm::StringRef.getAsInteger().
While at it, change the port type from int32_t to uint16_t. The method
never returns any value outside this range, and using the correct type
allows us to rely on getAsInteger()'s implicit overflow check.
https://reviews.llvm.org/D110391
Files:
lldb/include/lldb/Host/Socket.h
lldb/source/Host/common/Socket.cpp
lldb/source/Host/common/TCPSocket.cpp
lldb/source/Host/common/UDPSocket.cpp
lldb/tools/lldb-server/Acceptor.cpp
lldb/unittests/Host/SocketTest.cpp
Index: lldb/unittests/Host/SocketTest.cpp
===================================================================
--- lldb/unittests/Host/SocketTest.cpp
+++ lldb/unittests/Host/SocketTest.cpp
@@ -34,7 +34,7 @@
TEST_P(SocketTest, DecodeHostAndPort) {
std::string host_str;
std::string port_str;
- int32_t port;
+ uint16_t port;
Status error;
EXPECT_TRUE(Socket::DecodeHostAndPort("localhost:1138", host_str, port_str,
port, &error));
Index: lldb/tools/lldb-server/Acceptor.cpp
===================================================================
--- lldb/tools/lldb-server/Acceptor.cpp
+++ lldb/tools/lldb-server/Acceptor.cpp
@@ -96,7 +96,7 @@
} else {
std::string host_str;
std::string port_str;
- int32_t port = INT32_MIN;
+ uint16_t port;
// Try to match socket name as $host:port - e.g., localhost:5555
if (Socket::DecodeHostAndPort(name, host_str, port_str, port, nullptr))
socket_protocol = Socket::ProtocolTcp;
Index: lldb/source/Host/common/UDPSocket.cpp
===================================================================
--- lldb/source/Host/common/UDPSocket.cpp
+++ lldb/source/Host/common/UDPSocket.cpp
@@ -63,7 +63,7 @@
Status error;
std::string host_str;
std::string port_str;
- int32_t port = INT32_MIN;
+ uint16_t port;
if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
return error.ToError();
Index: lldb/source/Host/common/TCPSocket.cpp
===================================================================
--- lldb/source/Host/common/TCPSocket.cpp
+++ lldb/source/Host/common/TCPSocket.cpp
@@ -156,7 +156,7 @@
Status error;
std::string host_str;
std::string port_str;
- int32_t port = INT32_MIN;
+ uint16_t port;
if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
return error;
@@ -192,7 +192,7 @@
Status error;
std::string host_str;
std::string port_str;
- int32_t port = INT32_MIN;
+ uint16_t port;
if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
return error;
Index: lldb/source/Host/common/Socket.cpp
===================================================================
--- lldb/source/Host/common/Socket.cpp
+++ lldb/source/Host/common/Socket.cpp
@@ -11,11 +11,9 @@
#include "lldb/Host/Config.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/SocketAddress.h"
-#include "lldb/Host/StringConvert.h"
#include "lldb/Host/common/TCPSocket.h"
#include "lldb/Host/common/UDPSocket.h"
#include "lldb/Utility/Log.h"
-#include "lldb/Utility/RegularExpression.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Errno.h"
@@ -175,7 +173,7 @@
Status error;
std::string host_str;
std::string port_str;
- int32_t port = INT32_MIN;
+ uint16_t port;
if (!DecodeHostAndPort(host_and_port, host_str, port_str, port, &error))
return error.ToError();
@@ -274,39 +272,28 @@
bool Socket::DecodeHostAndPort(llvm::StringRef host_and_port,
std::string &host_str, std::string &port_str,
- int32_t &port, Status *error_ptr) {
- static RegularExpression g_regex(
- llvm::StringRef("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)"));
+ uint16_t &port, Status *error_ptr) {
+ static llvm::Regex g_regex("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
llvm::SmallVector<llvm::StringRef, 3> matches;
- if (g_regex.Execute(host_and_port, &matches)) {
+ if (error_ptr)
+ error_ptr->Clear();
+ if (g_regex.match(host_and_port, &matches)) {
host_str = matches[1].str();
port_str = matches[2].str();
// IPv6 addresses are wrapped in [] when specified with ports
if (host_str.front() == '[' && host_str.back() == ']')
host_str = host_str.substr(1, host_str.size() - 2);
- bool ok = false;
- port = StringConvert::ToUInt32(port_str.c_str(), UINT32_MAX, 10, &ok);
- if (ok && port <= UINT16_MAX) {
- if (error_ptr)
- error_ptr->Clear();
+ if (!matches[2].getAsInteger(10, port))
+ return true;
+ } else {
+ // If this was unsuccessful, then check if it's simply a signed 32-bit
+ // integer, representing a port with an empty host.
+ host_str.clear();
+ port_str.clear();
+ if (!host_and_port.getAsInteger(10, port)) {
+ port_str = host_and_port.str();
return true;
}
- // port is too large
- if (error_ptr)
- error_ptr->SetErrorStringWithFormat(
- "invalid host:port specification: '%s'", host_and_port.str().c_str());
- return false;
- }
-
- // If this was unsuccessful, then check if it's simply a signed 32-bit
- // integer, representing a port with an empty host.
- host_str.clear();
- port_str.clear();
- if (to_integer(host_and_port, port, 10) && port < UINT16_MAX) {
- port_str = std::string(host_and_port);
- if (error_ptr)
- error_ptr->Clear();
- return true;
}
if (error_ptr)
Index: lldb/include/lldb/Host/Socket.h
===================================================================
--- lldb/include/lldb/Host/Socket.h
+++ lldb/include/lldb/Host/Socket.h
@@ -105,7 +105,7 @@
static bool DecodeHostAndPort(llvm::StringRef host_and_port,
std::string &host_str, std::string &port_str,
- int32_t &port, Status *error_ptr);
+ uint16_t &port, Status *error_ptr);
// If this Socket is connected then return the URI used to connect.
virtual std::string GetRemoteConnectionURI() const { return ""; };
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits