From 6a8cff57309ba3168844640b2da2ea3103d594fd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 30 Apr 2018 22:55:05 +0000 Subject: [PATCH] os: fix missing break bug in earlier CL 110295's use of Uname The Uname name was never being used because it always generated a too-long string. The new test looking for zero bytes wouldn't have caught it (I thought it would've), but is still nice to have. Updates #24701 Change-Id: I2648074452609e4ad1b9736973e1b3a95eac658d Reviewed-on: https://go-review.googlesource.com/110436 Reviewed-by: Ian Lance Taylor --- src/os/os_test.go | 35 +++++++++++++++-------------------- src/os/sys_linux.go | 1 + 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/os/os_test.go b/src/os/os_test.go index af3a2fbee6..9d13fe05ac 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -1523,11 +1523,7 @@ func runBinHostname(t *testing.T) string { return output } -func testWindowsHostname(t *testing.T) { - hostname, err := Hostname() - if err != nil { - t.Fatal(err) - } +func testWindowsHostname(t *testing.T, hostname string) { cmd := osexec.Command("hostname") out, err := cmd.CombinedOutput() if err != nil { @@ -1535,27 +1531,30 @@ func testWindowsHostname(t *testing.T) { } want := strings.Trim(string(out), "\r\n") if hostname != want { - t.Fatalf("Hostname() = %q, want %q", hostname, want) + t.Fatalf("Hostname() = %q != system hostname of %q", hostname, want) } } func TestHostname(t *testing.T) { + hostname, err := Hostname() + if err != nil { + t.Fatal(err) + } + if hostname == "" { + t.Fatal("Hostname returned empty string and no error") + } + if strings.Contains(hostname, "\x00") { + t.Fatalf("unexpected zero byte in hostname: %q", hostname) + } + // There is no other way to fetch hostname on windows, but via winapi. // On Plan 9 it can be taken from #c/sysname as Hostname() does. switch runtime.GOOS { case "android", "plan9": - // No /bin/hostname to verify against, but at least - // verify we get something back from Hostname. - hostname, err := Hostname() - if err != nil { - t.Fatal(err) - } - if hostname == "" { - t.Fatal("Hostname returned empty string and no error") - } + // No /bin/hostname to verify against. return case "windows": - testWindowsHostname(t) + testWindowsHostname(t, hostname) return } @@ -1564,10 +1563,6 @@ func TestHostname(t *testing.T) { // Check internal Hostname() against the output of /bin/hostname. // Allow that the internal Hostname returns a Fully Qualified Domain Name // and the /bin/hostname only returns the first component - hostname, err := Hostname() - if err != nil { - t.Fatalf("%v", err) - } want := runBinHostname(t) if hostname != want { i := strings.Index(hostname, ".") diff --git a/src/os/sys_linux.go b/src/os/sys_linux.go index 3fbe5e9f82..467127653a 100644 --- a/src/os/sys_linux.go +++ b/src/os/sys_linux.go @@ -22,6 +22,7 @@ func hostname() (name string, err error) { buf[i] = uint8(b) if b == 0 { name = string(buf[:i]) + break } } // If we got a name and it's not potentially truncated -- 2.50.0