]> Cypherpunks repositories - gostls13.git/commitdiff
os: fix missing break bug in earlier CL 110295's use of Uname
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 30 Apr 2018 22:55:05 +0000 (22:55 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 1 May 2018 15:55:49 +0000 (15:55 +0000)
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 <iant@golang.org>
src/os/os_test.go
src/os/sys_linux.go

index af3a2fbee65a1302637e5887b996948794efa204..9d13fe05accc2e02b4b6619ab814f3a6f14d0795 100644 (file)
@@ -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, ".")
index 3fbe5e9f82cf678626d8e73ac463895977cfaf1a..467127653a8b7efeef3560f5e219beba94237f9b 100644 (file)
@@ -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