Skip to content

Commit c99fee0

Browse files
committed
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 <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alex Brainman <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 7ad92e9 commit c99fee0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/syscall/syscall_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ func Rename(oldpath, newpath string) (err error) {
585585
}
586586

587587
func ComputerName() (name string, err error) {
588-
b := make([]uint16, MAX_COMPUTERNAME_LENGTH+1)
589-
var n uint32
588+
var n uint32 = MAX_COMPUTERNAME_LENGTH + 1
589+
b := make([]uint16, n)
590590
e := GetComputerName(&b[0], &n)
591591
if e != nil {
592592
return "", e

src/syscall/syscall_windows_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func TestOpen_Dir(t *testing.T) {
3737
}
3838
}
3939

40+
func TestComputerName(t *testing.T) {
41+
name, err := syscall.ComputerName()
42+
if err != nil {
43+
t.Fatalf("ComputerName failed: %v", err)
44+
}
45+
if len(name) == 0 {
46+
t.Error("ComputerName returned empty string")
47+
}
48+
}
49+
4050
func TestWin32finddata(t *testing.T) {
4151
dir := t.TempDir()
4252

0 commit comments

Comments
 (0)