Skip to content

Commit 7bdbc05

Browse files
aamCommit Queue
authored and
Commit Queue
committed
[vm/win] Use wide-character api for local hostname on Windows.
Fixes #52701 TEST=test locally Change-Id: Ia38b67cadea3d15824b06d0a2fc69f0c20192bb6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310481 Commit-Queue: Alexander Aprelev <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent e4b7d10 commit 7bdbc05

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

runtime/bin/platform_win.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,14 @@ bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) {
218218
if (!SocketBase::Initialize()) {
219219
return false;
220220
}
221-
return gethostname(buffer, buffer_length) == 0;
221+
// 256 is max length per https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-gethostnamew#remarks
222+
const int HOSTNAME_MAXLENGTH = 256;
223+
wchar_t hostname_w[HOSTNAME_MAXLENGTH];
224+
if (GetHostNameW(hostname_w, HOSTNAME_MAXLENGTH) != 0) {
225+
return false;
226+
}
227+
return WideCharToMultiByte(CP_UTF8, 0, hostname_w, -1, buffer, buffer_length,
228+
nullptr, nullptr) != 0;
222229
#endif
223230
}
224231

0 commit comments

Comments
 (0)