Clear the output string passed to GetHostName()
authorAaron Smith <aaron.smith@microsoft.com>
Wed, 17 Apr 2019 03:13:06 +0000 (03:13 +0000)
committerAaron Smith <aaron.smith@microsoft.com>
Wed, 17 Apr 2019 03:13:06 +0000 (03:13 +0000)
LLVM's wchar to UTF8 conversion routine expects an empty string to store the output.
GetHostName() on Windows is sometimes called with a non-empty string which triggers
an assert. The simple fix is to clear the output string before the conversion.

llvm-svn: 358550

lldb/source/Host/windows/HostInfoWindows.cpp
lldb/unittests/Host/HostInfoTest.cpp

index 8cd8f2d..459703f 100644 (file)
@@ -95,6 +95,8 @@ bool HostInfoWindows::GetHostname(std::string &s) {
   if (!::GetComputerNameW(buffer, &dwSize))
     return false;
 
+  // The conversion requires an empty string.
+  s.clear();
   return llvm::convertWideToUTF8(buffer, s);
 }
 
index b4ba319..fb50e29 100644 (file)
@@ -50,3 +50,9 @@ TEST_F(HostInfoTest, GetAugmentedArchSpec) {
   EXPECT_EQ(HostInfo::GetAugmentedArchSpec(LLDB_ARCH_DEFAULT).GetTriple(),
             HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple());
 }
+
+TEST_F(HostInfoTest, GetHostname) {
+  // Check non-empty string input works correctly.
+  std::string s("abc");
+  EXPECT_TRUE(HostInfo::GetHostname(s));
+}