[Lldb-commits] [lldb] r355817 - Fix invalid use of StringRef::data in Socket::DecodeHostAndPort

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 11 03:34:57 PDT 2019


Author: labath
Date: Mon Mar 11 03:34:57 2019
New Revision: 355817

URL: http://llvm.org/viewvc/llvm-project?rev=355817&view=rev
Log:
Fix invalid use of StringRef::data in Socket::DecodeHostAndPort

the input StringRef is not guaranteed to be null-terminated, so using
data to get the c string is wrong. Luckily, in two of the usages the
target function already accepts a StringRef so we can just drop the
data() call, and the third one is easily replaced by a stringref-aware
function.

Issue found by msan.

Modified:
    lldb/trunk/source/Host/common/Socket.cpp

Modified: lldb/trunk/source/Host/common/Socket.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Socket.cpp?rev=355817&r1=355816&r2=355817&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Socket.cpp (original)
+++ lldb/trunk/source/Host/common/Socket.cpp Mon Mar 11 03:34:57 2019
@@ -259,8 +259,8 @@ bool Socket::DecodeHostAndPort(llvm::Str
       llvm::StringRef("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)"));
   RegularExpression::Match regex_match(2);
   if (g_regex.Execute(host_and_port, &regex_match)) {
-    if (regex_match.GetMatchAtIndex(host_and_port.data(), 1, host_str) &&
-        regex_match.GetMatchAtIndex(host_and_port.data(), 2, port_str)) {
+    if (regex_match.GetMatchAtIndex(host_and_port, 1, host_str) &&
+        regex_match.GetMatchAtIndex(host_and_port, 2, port_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);
@@ -283,9 +283,7 @@ bool Socket::DecodeHostAndPort(llvm::Str
   // integer, representing a port with an empty host.
   host_str.clear();
   port_str.clear();
-  bool ok = false;
-  port = StringConvert::ToUInt32(host_and_port.data(), UINT32_MAX, 10, &ok);
-  if (ok && port < UINT16_MAX) {
+  if (to_integer(host_and_port, port, 10) && port < UINT16_MAX) {
     port_str = host_and_port;
     if (error_ptr)
       error_ptr->Clear();




More information about the lldb-commits mailing list