Clear the output string passed to GetHostName()
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.
git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@358550 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Host/windows/HostInfoWindows.cpp b/source/Host/windows/HostInfoWindows.cpp
index 8cd8f2d..459703f 100644
--- a/source/Host/windows/HostInfoWindows.cpp
+++ b/source/Host/windows/HostInfoWindows.cpp
@@ -95,6 +95,8 @@
if (!::GetComputerNameW(buffer, &dwSize))
return false;
+ // The conversion requires an empty string.
+ s.clear();
return llvm::convertWideToUTF8(buffer, s);
}
diff --git a/unittests/Host/HostInfoTest.cpp b/unittests/Host/HostInfoTest.cpp
index b4ba319..fb50e29 100644
--- a/unittests/Host/HostInfoTest.cpp
+++ b/unittests/Host/HostInfoTest.cpp
@@ -50,3 +50,9 @@
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));
+}