From 245f7fdcfa9fb4351f53ab9d96b31e1e472e81bd Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 17 Nov 2016 01:38:02 +0000 Subject: [PATCH] Convert UriParser to use StringRef. llvm-svn: 287190 --- lldb/include/lldb/Core/UUID.h | 1 + lldb/source/Core/UUID.cpp | 6 ++-- lldb/source/Interpreter/OptionValueUUID.cpp | 5 +-- lldb/source/Plugins/Platform/Android/AdbClient.cpp | 4 +-- lldb/source/Plugins/Platform/Android/AdbClient.h | 2 +- .../Plugins/Platform/Android/PlatformAndroid.cpp | 2 +- .../Android/PlatformAndroidRemoteGDBServer.cpp | 14 ++++---- .../Android/PlatformAndroidRemoteGDBServer.h | 2 +- .../gdb-server/PlatformRemoteGDBServer.cpp | 7 ++-- .../GDBRemoteCommunicationServerPlatform.cpp | 8 ++--- lldb/source/Utility/UriParser.cpp | 36 ++++++++------------ lldb/source/Utility/UriParser.h | 8 +++-- lldb/unittests/Utility/UriParserTest.cpp | 38 +++++++++++++++++----- 13 files changed, 77 insertions(+), 56 deletions(-) diff --git a/lldb/include/lldb/Core/UUID.h b/lldb/include/lldb/Core/UUID.h index de1e697..7cf5cf3 100644 --- a/lldb/include/lldb/Core/UUID.h +++ b/lldb/include/lldb/Core/UUID.h @@ -73,6 +73,7 @@ public: //------------------------------------------------------------------ static llvm::StringRef DecodeUUIDBytesFromString(llvm::StringRef str, ValueType &uuid_bytes, + uint32_t &bytes_decoded, uint32_t num_uuid_bytes = 16); protected: diff --git a/lldb/source/Core/UUID.cpp b/lldb/source/Core/UUID.cpp index d7ebea72..a08a748 100644 --- a/lldb/source/Core/UUID.cpp +++ b/lldb/source/Core/UUID.cpp @@ -128,6 +128,7 @@ static inline int xdigit_to_int(char ch) { llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p, ValueType &uuid_bytes, + uint32_t &bytes_decoded, uint32_t num_uuid_bytes) { ::memset(uuid_bytes, 0, sizeof(uuid_bytes)); size_t uuid_byte_idx = 0; @@ -157,6 +158,7 @@ llvm::StringRef UUID::DecodeUUIDBytesFromString(llvm::StringRef p, // Clear trailing bytes to 0. for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++) uuid_bytes[i] = 0; + bytes_decoded = uuid_byte_idx; return p; } size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) { @@ -169,9 +171,9 @@ size_t UUID::SetFromCString(const char *cstr, uint32_t num_uuid_bytes) { // Skip leading whitespace characters p = p.ltrim(); + uint32_t bytes_decoded = 0; llvm::StringRef rest = - UUID::DecodeUUIDBytesFromString(p, m_uuid, num_uuid_bytes); - size_t bytes_decoded = p.size() - rest.size(); + UUID::DecodeUUIDBytesFromString(p, m_uuid, bytes_decoded, num_uuid_bytes); // If we successfully decoded a UUID, return the amount of characters that // were consumed diff --git a/lldb/source/Interpreter/OptionValueUUID.cpp b/lldb/source/Interpreter/OptionValueUUID.cpp index bd6858c..a1f16e1 100644 --- a/lldb/source/Interpreter/OptionValueUUID.cpp +++ b/lldb/source/Interpreter/OptionValueUUID.cpp @@ -79,8 +79,9 @@ size_t OptionValueUUID::AutoComplete(CommandInterpreter &interpreter, const size_t num_modules = target->GetImages().GetSize(); if (num_modules > 0) { UUID::ValueType uuid_bytes; - llvm::StringRef rest = UUID::DecodeUUIDBytesFromString(s, uuid_bytes); - const size_t num_bytes_decoded = s.size() - rest.size(); + size_t num_bytes_decoded = 0; + llvm::StringRef rest = + UUID::DecodeUUIDBytesFromString(s, uuid_bytes, num_bytes_decoded); for (size_t i = 0; i < num_modules; ++i) { ModuleSP module_sp(target->GetImages().GetModuleAtIndex(i)); if (module_sp) { diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.cpp b/lldb/source/Plugins/Platform/Android/AdbClient.cpp index 7c9c70a..fbf6dee 100644 --- a/lldb/source/Plugins/Platform/Android/AdbClient.cpp +++ b/lldb/source/Plugins/Platform/Android/AdbClient.cpp @@ -185,7 +185,7 @@ Error AdbClient::SetPortForwarding(const uint16_t local_port, } Error AdbClient::SetPortForwarding(const uint16_t local_port, - const char *remote_socket_name, + llvm::StringRef remote_socket_name, const UnixSocketNamespace socket_namespace) { char message[PATH_MAX]; const char *sock_namespace_str = @@ -193,7 +193,7 @@ Error AdbClient::SetPortForwarding(const uint16_t local_port, ? kSocketNamespaceAbstract : kSocketNamespaceFileSystem; snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port, - sock_namespace_str, remote_socket_name); + sock_namespace_str, remote_socket_name.str().c_str()); const auto error = SendDeviceMessage(message); if (error.Fail()) diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.h b/lldb/source/Plugins/Platform/Android/AdbClient.h index 471dce5..56bb04e 100644 --- a/lldb/source/Plugins/Platform/Android/AdbClient.h +++ b/lldb/source/Plugins/Platform/Android/AdbClient.h @@ -96,7 +96,7 @@ public: const uint16_t remote_port); Error SetPortForwarding(const uint16_t local_port, - const char *remote_socket_name, + llvm::StringRef remote_socket_name, const UnixSocketNamespace socket_namespace); Error DeletePortForwarding(const uint16_t local_port); diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp index 9f1a389..7d70172 100644 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp @@ -168,7 +168,7 @@ Error PlatformAndroid::ConnectRemote(Args &args) { m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer()); int port; - std::string scheme, host, path; + llvm::StringRef scheme, host, path; const char *url = args.GetArgumentAtIndex(0); if (!url) return Error("URL is null."); diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp index 9650fb4..288accf 100644 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp @@ -27,7 +27,7 @@ static const lldb::pid_t g_remote_platform_pid = static Error ForwardPortWithAdb( const uint16_t local_port, const uint16_t remote_port, - const char *remote_socket_name, + llvm::StringRef remote_socket_name, const llvm::Optional &socket_namespace, std::string &device_id) { Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); @@ -50,7 +50,7 @@ static Error ForwardPortWithAdb( if (log) log->Printf("Forwarding remote socket \"%s\" to local TCP port %d", - remote_socket_name, local_port); + remote_socket_name.str().c_str(), local_port); if (!socket_namespace) return Error("Invalid socket namespace"); @@ -114,7 +114,7 @@ Error PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) { return Error("\"platform connect\" takes a single argument: "); int remote_port; - std::string scheme, host, path; + llvm::StringRef scheme, host, path; const char *url = args.GetArgumentAtIndex(0); if (!url) return Error("URL is null."); @@ -132,7 +132,7 @@ Error PlatformAndroidRemoteGDBServer::ConnectRemote(Args &args) { std::string connect_url; auto error = MakeConnectURL(g_remote_platform_pid, (remote_port < 0) ? 0 : remote_port, - path.c_str(), connect_url); + path, connect_url); if (error.Fail()) return error; @@ -175,7 +175,7 @@ void PlatformAndroidRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { Error PlatformAndroidRemoteGDBServer::MakeConnectURL( const lldb::pid_t pid, const uint16_t remote_port, - const char *remote_socket_name, std::string &connect_url) { + llvm::StringRef remote_socket_name, std::string &connect_url) { static const int kAttempsNum = 5; Error error; @@ -214,7 +214,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess( static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL; int remote_port; - std::string scheme, host, path; + llvm::StringRef scheme, host, path; if (!UriParser::Parse(connect_url, scheme, host, remote_port, path)) { error.SetErrorStringWithFormat("Invalid URL: %s", connect_url); return nullptr; @@ -222,7 +222,7 @@ lldb::ProcessSP PlatformAndroidRemoteGDBServer::ConnectProcess( std::string new_connect_url; error = MakeConnectURL(s_remote_gdbserver_fake_pid--, - (remote_port < 0) ? 0 : remote_port, path.c_str(), + (remote_port < 0) ? 0 : remote_port, path, new_connect_url); if (error.Fail()) return nullptr; diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h index 9748c86..f9593fa 100644 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h @@ -55,7 +55,7 @@ protected: void DeleteForwardPort(lldb::pid_t pid); Error MakeConnectURL(const lldb::pid_t pid, const uint16_t remote_port, - const char *remote_socket_name, + llvm::StringRef remote_socket_name, std::string &connect_url); private: diff --git a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp index 1a5df93..ccc7301 100644 --- a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp @@ -309,9 +309,12 @@ Error PlatformRemoteGDBServer::ConnectRemote(Args &args) { const char *url = args.GetArgumentAtIndex(0); if (!url) return Error("URL is null."); - if (!UriParser::Parse(url, m_platform_scheme, m_platform_hostname, port, - path)) + llvm::StringRef scheme, hostname, pathname; + if (!UriParser::Parse(url, scheme, hostname, port, pathname)) return Error("Invalid URL: %s", url); + m_platform_scheme = scheme; + m_platform_hostname = hostname; + path = pathname; const ConnectionStatus status = m_gdb_client.Connect(url, &error); if (status == eConnectionStatusSuccess) { diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp index 8bf48e9..1106974 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp @@ -124,10 +124,10 @@ Error GDBRemoteCommunicationServerPlatform::LaunchGDBServer( this, std::placeholders::_1), false); - std::string platform_scheme; - std::string platform_ip; + llvm::StringRef platform_scheme; + llvm::StringRef platform_ip; int platform_port; - std::string platform_path; + llvm::StringRef platform_path; bool ok = UriParser::Parse(GetConnection()->GetURI(), platform_scheme, platform_ip, platform_port, platform_path); UNUSED_IF_ASSERT_DISABLED(ok); @@ -140,7 +140,7 @@ Error GDBRemoteCommunicationServerPlatform::LaunchGDBServer( #endif uint16_t *port_ptr = &port; if (m_socket_protocol == Socket::ProtocolTcp) - url << platform_ip << ":" << port; + url << platform_ip.str() << ":" << port; else { socket_name = GetDomainSocketPath("gdbserver").GetPath(); url << socket_name; diff --git a/lldb/source/Utility/UriParser.cpp b/lldb/source/Utility/UriParser.cpp index 6a1f030..a1d6e4c 100644 --- a/lldb/source/Utility/UriParser.cpp +++ b/lldb/source/Utility/UriParser.cpp @@ -23,18 +23,19 @@ using namespace lldb_private; //---------------------------------------------------------------------- // UriParser::Parse //---------------------------------------------------------------------- -bool UriParser::Parse(const std::string &uri, std::string &scheme, - std::string &hostname, int &port, std::string &path) { - std::string tmp_scheme, tmp_hostname, tmp_port, tmp_path; +bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme, + llvm::StringRef &hostname, int &port, + llvm::StringRef &path) { + llvm::StringRef tmp_scheme, tmp_hostname, tmp_port, tmp_path; - static const char *kSchemeSep = "://"; + const llvm::StringRef kSchemeSep("://"); auto pos = uri.find(kSchemeSep); if (pos == std::string::npos) return false; // Extract path. tmp_scheme = uri.substr(0, pos); - auto host_pos = pos + strlen(kSchemeSep); + auto host_pos = pos + kSchemeSep.size(); auto path_pos = uri.find('/', host_pos); if (path_pos != std::string::npos) tmp_path = uri.substr(path_pos); @@ -53,28 +54,19 @@ bool UriParser::Parse(const std::string &uri, std::string &scheme, return false; tmp_hostname = host_port.substr(1, pos - 1); - host_port.erase(0, pos + 1); + host_port = host_port.drop_front(pos + 1); + if (!host_port.empty() && !host_port.consume_front(":")) + return false; } else { - pos = host_port.find(':'); - tmp_hostname = host_port.substr( - 0, (pos != std::string::npos) ? pos : host_port.size()); - host_port.erase(0, (pos != std::string::npos) ? pos : host_port.size()); + std::tie(tmp_hostname, host_port) = host_port.split(':'); } // Extract port - tmp_port = host_port; - if (!tmp_port.empty()) { - if (tmp_port[0] != ':') - return false; - tmp_port = tmp_port.substr(1); - bool success = false; - auto port_tmp = - StringConvert::ToUInt32(tmp_port.c_str(), UINT32_MAX, 10, &success); - if (!success || port_tmp > 65535) { - // there are invalid characters in port_buf + if (!host_port.empty()) { + uint16_t port_value = 0; + if (host_port.getAsInteger(0, port_value)) return false; - } - port = port_tmp; + port = port_value; } else port = -1; diff --git a/lldb/source/Utility/UriParser.h b/lldb/source/Utility/UriParser.h index 56bd194..7ebf76f 100644 --- a/lldb/source/Utility/UriParser.h +++ b/lldb/source/Utility/UriParser.h @@ -12,9 +12,10 @@ // C Includes // C++ Includes -#include // Other libraries and framework includes +#include "llvm/ADT/StringRef.h" + // Project includes class UriParser { @@ -27,8 +28,9 @@ public: // // if the url is invalid, function returns false and // output parameters remain unchanged - static bool Parse(const std::string &uri, std::string &scheme, - std::string &hostname, int &port, std::string &path); + static bool Parse(llvm::StringRef uri, llvm::StringRef &scheme, + llvm::StringRef &hostname, int &port, + llvm::StringRef &path); }; #endif // utility_UriParser_h_ diff --git a/lldb/unittests/Utility/UriParserTest.cpp b/lldb/unittests/Utility/UriParserTest.cpp index 2fd1960..8b08e63 100644 --- a/lldb/unittests/Utility/UriParserTest.cpp +++ b/lldb/unittests/Utility/UriParserTest.cpp @@ -30,16 +30,16 @@ public: }; #define VALIDATE \ - std::string scheme(kAsdf); \ - std::string hostname(kAsdf); \ + llvm::StringRef scheme(kAsdf); \ + llvm::StringRef hostname(kAsdf); \ int port(1138); \ - std::string path(kAsdf); \ + llvm::StringRef path(kAsdf); \ EXPECT_EQ(testCase.m_result, \ UriParser::Parse(testCase.m_uri, scheme, hostname, port, path)); \ - EXPECT_STREQ(testCase.m_scheme, scheme.c_str()); \ - EXPECT_STREQ(testCase.m_hostname, hostname.c_str()); \ + EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str()); \ + EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str()); \ EXPECT_EQ(testCase.m_port, port); \ - EXPECT_STREQ(testCase.m_path, path.c_str()); + EXPECT_STREQ(testCase.m_path, path.str().c_str()); TEST_F(UriParserTest, Minimal) { const UriTestCase testCase("x://y", "x", "y", -1, "/"); @@ -48,7 +48,17 @@ TEST_F(UriParserTest, Minimal) { TEST_F(UriParserTest, MinimalPort) { const UriTestCase testCase("x://y:1", "x", "y", 1, "/"); - VALIDATE + llvm::StringRef scheme(kAsdf); + llvm::StringRef hostname(kAsdf); + int port(1138); + llvm::StringRef path(kAsdf); + bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path); + EXPECT_EQ(testCase.m_result, result); + + EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str()); + EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str()); + EXPECT_EQ(testCase.m_port, port); + EXPECT_STREQ(testCase.m_path, path.str().c_str()); } TEST_F(UriParserTest, MinimalPath) { @@ -69,13 +79,23 @@ TEST_F(UriParserTest, LongPath) { TEST_F(UriParserTest, TypicalPortPath) { const UriTestCase testCase("connect://192.168.100.132:5432/", "connect", "192.168.100.132", 5432, "/"); - VALIDATE + VALIDATE; } TEST_F(UriParserTest, BracketedHostnamePort) { const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect", "192.168.100.132", 5432, "/"); - VALIDATE + llvm::StringRef scheme(kAsdf); + llvm::StringRef hostname(kAsdf); + int port(1138); + llvm::StringRef path(kAsdf); + bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path); + EXPECT_EQ(testCase.m_result, result); + + EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str()); + EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str()); + EXPECT_EQ(testCase.m_port, port); + EXPECT_STREQ(testCase.m_path, path.str().c_str()); } TEST_F(UriParserTest, BracketedHostname) { -- 2.7.4