Skip to content

Commit

Permalink
Reapply "[vm/win] Use wide-character api for local hostname on Windows."
Browse files Browse the repository at this point in the history
This relands a fix for looking up hostname on Windows in utf8-aware fashion.
Previously this was reverted because the API was only introduced in Windows 8,
making this use of Windows API fail on Windows 7.
With Windows 7 no longer being a target, original fix can be relanded.

BUG=#52701
TEST=manually

Change-Id: I330048c8b92ce805b7bb7e3fa05c88fefe3a30b0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353500
Reviewed-by: Siva Annamalai <[email protected]>
Commit-Queue: Alexander Aprelev <[email protected]>
  • Loading branch information
aam authored and Commit Queue committed Feb 21, 2024
1 parent ec4dd93 commit 35c6cc6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion runtime/bin/platform_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,14 @@ bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) {
if (!SocketBase::Initialize()) {
return false;
}
return gethostname(buffer, buffer_length) == 0;
// 256 is max length per https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-gethostnamew#remarks
const int HOSTNAME_MAXLENGTH = 256;
wchar_t hostname_w[HOSTNAME_MAXLENGTH];
if (GetHostNameW(hostname_w, HOSTNAME_MAXLENGTH) != 0) {
return false;
}
return WideCharToMultiByte(CP_UTF8, 0, hostname_w, -1, buffer, buffer_length,
nullptr, nullptr) != 0;
#endif
}

Expand Down

0 comments on commit 35c6cc6

Please sign in to comment.