Skip to content

Commit 6a8cff5

Browse files
committed
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 <[email protected]>
1 parent d469809 commit 6a8cff5

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/os/os_test.go

+15-20
Original file line numberDiff line numberDiff line change
@@ -1523,39 +1523,38 @@ func runBinHostname(t *testing.T) string {
15231523
return output
15241524
}
15251525

1526-
func testWindowsHostname(t *testing.T) {
1527-
hostname, err := Hostname()
1528-
if err != nil {
1529-
t.Fatal(err)
1530-
}
1526+
func testWindowsHostname(t *testing.T, hostname string) {
15311527
cmd := osexec.Command("hostname")
15321528
out, err := cmd.CombinedOutput()
15331529
if err != nil {
15341530
t.Fatalf("Failed to execute hostname command: %v %s", err, out)
15351531
}
15361532
want := strings.Trim(string(out), "\r\n")
15371533
if hostname != want {
1538-
t.Fatalf("Hostname() = %q, want %q", hostname, want)
1534+
t.Fatalf("Hostname() = %q != system hostname of %q", hostname, want)
15391535
}
15401536
}
15411537

15421538
func TestHostname(t *testing.T) {
1539+
hostname, err := Hostname()
1540+
if err != nil {
1541+
t.Fatal(err)
1542+
}
1543+
if hostname == "" {
1544+
t.Fatal("Hostname returned empty string and no error")
1545+
}
1546+
if strings.Contains(hostname, "\x00") {
1547+
t.Fatalf("unexpected zero byte in hostname: %q", hostname)
1548+
}
1549+
15431550
// There is no other way to fetch hostname on windows, but via winapi.
15441551
// On Plan 9 it can be taken from #c/sysname as Hostname() does.
15451552
switch runtime.GOOS {
15461553
case "android", "plan9":
1547-
// No /bin/hostname to verify against, but at least
1548-
// verify we get something back from Hostname.
1549-
hostname, err := Hostname()
1550-
if err != nil {
1551-
t.Fatal(err)
1552-
}
1553-
if hostname == "" {
1554-
t.Fatal("Hostname returned empty string and no error")
1555-
}
1554+
// No /bin/hostname to verify against.
15561555
return
15571556
case "windows":
1558-
testWindowsHostname(t)
1557+
testWindowsHostname(t, hostname)
15591558
return
15601559
}
15611560

@@ -1564,10 +1563,6 @@ func TestHostname(t *testing.T) {
15641563
// Check internal Hostname() against the output of /bin/hostname.
15651564
// Allow that the internal Hostname returns a Fully Qualified Domain Name
15661565
// and the /bin/hostname only returns the first component
1567-
hostname, err := Hostname()
1568-
if err != nil {
1569-
t.Fatalf("%v", err)
1570-
}
15711566
want := runBinHostname(t)
15721567
if hostname != want {
15731568
i := strings.Index(hostname, ".")

src/os/sys_linux.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func hostname() (name string, err error) {
2222
buf[i] = uint8(b)
2323
if b == 0 {
2424
name = string(buf[:i])
25+
break
2526
}
2627
}
2728
// If we got a name and it's not potentially truncated

0 commit comments

Comments
 (0)