Skip to content

Commit

Permalink
syscall: fix ComputerName on Windows
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
qmuntal committed May 30, 2023
1 parent 7ad92e9 commit c99fee0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/syscall/syscall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c99fee0

Please sign in to comment.