]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: fix ComputerName on Windows
authorqmuntal <quimmuntal@gmail.com>
Mon, 29 May 2023 09:45:14 +0000 (11:45 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Tue, 30 May 2023 13:48:54 +0000 (13:48 +0000)
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 <quimmuntal@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/syscall/syscall_windows.go
src/syscall/syscall_windows_test.go

index d721309781d2dbfa0627a1540a838c36eeca30b6..9f1b384de4362a1bf33197a77694244c4f021ea7 100644 (file)
@@ -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
index 23041ee09a00c0e939df6f16c0fb44ed3bcd1346..7b31a863c3d4906995230ce7616cfd4c02be95c3 100644 (file)
@@ -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()