From 43aa565f14442fd4d63aec1df41c0357605db5fa Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Tue, 22 Oct 2024 13:35:51 -0700 Subject: [PATCH] Fix GetOsDeviceName returns string with trailing \0. PiperOrigin-RevId: 688673080 --- internal/platform/implementation/windows/BUILD | 1 + .../implementation/windows/device_info.cc | 15 ++++++++++----- .../implementation/windows/device_info_test.cc | 9 +++++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 5ebadaf8..1639b12f 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -53,6 +53,7 @@ cc_library( "//internal/platform:logging", "//internal/platform:uuid", "//internal/platform/implementation:types", + "//internal/platform/implementation/windows:string_utils", "//internal/platform/implementation/windows/generated:types", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index 236ce63b..cdb51c7b 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -28,6 +28,7 @@ #include "internal/base/files.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/windows/generated/winrt/base.h" +#include "internal/platform/implementation/windows/string_utils.h" #include "internal/platform/logging.h" #include "winrt/Windows.Foundation.Collections.h" #include "winrt/Windows.Foundation.h" @@ -43,6 +44,8 @@ using UserType = winrt::Windows::System::UserType; using UserAuthenticationStatus = winrt::Windows::System::UserAuthenticationStatus; +using ::nearby::windows::string_utils::WideStringToString; + template using IVectorView = winrt::Windows::Foundation::Collections::IVectorView; @@ -57,18 +60,20 @@ std::optional DeviceInfo::GetOsDeviceName() const { DWORD size = 0; // Get length of the computer name. - if (!GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size)) { + if (GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size) == 0) { if (GetLastError() != ERROR_MORE_DATA) { LOG(ERROR) << ": Failed to get device name size, error:" << GetLastError(); return std::nullopt; } } - std::wstring device_name(size, L' '); - if (GetComputerNameExW(ComputerNameDnsHostname, device_name.data(), &size)) { - winrt::hstring device_name_str(device_name); - return winrt::to_string(device_name_str); + if (GetComputerNameExW(ComputerNameDnsHostname, device_name.data(), &size) != + 0) { + // On input size includes null termination. + // On output size excludes null termination. + device_name.resize(size); + return WideStringToString(device_name); } LOG(ERROR) << ": Failed to get device name, error:" << GetLastError(); diff --git a/internal/platform/implementation/windows/device_info_test.cc b/internal/platform/implementation/windows/device_info_test.cc index 59f344c4..0fa23b26 100644 --- a/internal/platform/implementation/windows/device_info_test.cc +++ b/internal/platform/implementation/windows/device_info_test.cc @@ -14,6 +14,8 @@ #include "internal/platform/implementation/windows/device_info.h" +#include +#include #include "gtest/gtest.h" #include "internal/platform/implementation/device_info.h" @@ -22,8 +24,11 @@ namespace nearby { namespace windows { namespace { -TEST(DeviceInfo, DISABLED_GetComputerName) { - EXPECT_TRUE(DeviceInfo().GetOsDeviceName().has_value()); +TEST(DeviceInfo, GetComputerName) { + ASSERT_TRUE(DeviceInfo().GetOsDeviceName().has_value()); + std::string device_name = DeviceInfo().GetOsDeviceName().value(); + // Makes sure device_name does not include terminating null character. + EXPECT_EQ(device_name.size(), std::strlen(device_name.data())); } TEST(DeviceInfo, DISABLED_GetDeviceType) {