From: qmuntal Date: Mon, 29 May 2023 09:45:14 +0000 (+0200) Subject: syscall: fix ComputerName on Windows X-Git-Tag: go1.21rc1~196 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c99fee0545ed1f754ab28fdfeb5623f0cb5a5b40;p=gostls13.git syscall: fix ComputerName on Windows GetComputerName expects n to be the size of the buffer, and on output contains the number of characters copied to the buffer. CL 493036 broke ComputerName by always setting n to 0. Change-Id: I3f4b30d2f9825d321a6d28ec82bdc7b6294e04e4 Reviewed-on: https://go-review.googlesource.com/c/go/+/499035 Run-TryBot: Quim Muntal Reviewed-by: Dmitri Shuralyov TryBot-Result: Gopher Robot Reviewed-by: Alex Brainman Reviewed-by: Bryan Mills --- diff --git a/src/syscall/syscall_windows.go b/src/syscall/syscall_windows.go index d721309781..9f1b384de4 100644 --- a/src/syscall/syscall_windows.go +++ b/src/syscall/syscall_windows.go @@ -585,8 +585,8 @@ func Rename(oldpath, newpath string) (err error) { } func ComputerName() (name string, err error) { - b := make([]uint16, MAX_COMPUTERNAME_LENGTH+1) - var n uint32 + var n uint32 = MAX_COMPUTERNAME_LENGTH + 1 + b := make([]uint16, n) e := GetComputerName(&b[0], &n) if e != nil { return "", e diff --git a/src/syscall/syscall_windows_test.go b/src/syscall/syscall_windows_test.go index 23041ee09a..7b31a863c3 100644 --- a/src/syscall/syscall_windows_test.go +++ b/src/syscall/syscall_windows_test.go @@ -37,6 +37,16 @@ func TestOpen_Dir(t *testing.T) { } } +func TestComputerName(t *testing.T) { + name, err := syscall.ComputerName() + if err != nil { + t.Fatalf("ComputerName failed: %v", err) + } + if len(name) == 0 { + t.Error("ComputerName returned empty string") + } +} + func TestWin32finddata(t *testing.T) { dir := t.TempDir()