Skip to content

Commit af86efb

Browse files
ianlancetaylorgopherbot
authored andcommitted
os: treat Getwd result of ENOMEM the same as ENAMETOOLONG
We can see ENOMEM on FreeBSD. Also don't fail the test if we get an EPERM error when reading all the way up the tree; on Android we get that, perhaps because the root directory is unreadable. Also accept an EFAULT from a stat of a long name on Dragonfly, which we see on the builders. Change-Id: If37e6bf414b7b568c9a06130f71e79af153bfb75 Reviewed-on: https://go-review.googlesource.com/c/go/+/610415 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent a00195d commit af86efb

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/os/error_errno.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ import "syscall"
1010

1111
type syscallErrorType = syscall.Errno
1212

13-
const errENOSYS = syscall.ENOSYS
14-
const errERANGE = syscall.ERANGE
13+
const (
14+
errENOSYS = syscall.ENOSYS
15+
errERANGE = syscall.ERANGE
16+
errENOMEM = syscall.ENOMEM
17+
)

src/os/error_plan9.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ type syscallErrorType = syscall.ErrorString
1010

1111
var errENOSYS = syscall.NewError("function not implemented")
1212
var errERANGE = syscall.NewError("out of range")
13+
var errENOMEM = syscall.NewError("cannot allocate memory")

src/os/getwd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ func Getwd() (dir string, err error) {
6060
}
6161
}
6262
// Linux returns ENAMETOOLONG if the result is too long.
63-
// BSD systems appear to return EINVAL.
63+
// Some BSD systems appear to return EINVAL.
64+
// FreeBSD systems appear to use ENOMEM
6465
// Solaris appears to use ERANGE.
65-
if err != syscall.ENAMETOOLONG && err != syscall.EINVAL && err != errERANGE {
66+
if err != syscall.ENAMETOOLONG && err != syscall.EINVAL && err != errERANGE && err != errENOMEM {
6667
return dir, NewSyscallError("getwd", err)
6768
}
6869
}

src/os/getwd_unix_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package os_test
99
import (
1010
"errors"
1111
. "os"
12+
"runtime"
1213
"strings"
1314
"syscall"
1415
"testing"
@@ -56,6 +57,12 @@ func testGetwdDeep(t *testing.T, setPWD bool) {
5657
wd, err := Getwd()
5758
t.Logf("Getwd len: %d", len(wd))
5859
if err != nil {
60+
// We can get an EPERM error if we can't read up
61+
// to root, which happens on the Android builders.
62+
if errors.Is(err, syscall.EPERM) {
63+
t.Logf("ignoring EPERM error: %v", err)
64+
break
65+
}
5966
t.Fatal(err)
6067
}
6168
if setPWD && wd != dir {
@@ -72,7 +79,13 @@ func testGetwdDeep(t *testing.T, setPWD bool) {
7279
// all Unix platforms (4096, on Linux).
7380
if _, err := Stat(wd); err != nil || len(wd) > 4096 {
7481
t.Logf("Done; len(wd)=%d", len(wd))
75-
if err != nil && !errors.Is(err, syscall.ENAMETOOLONG) {
82+
// Most systems return ENAMETOOLONG.
83+
// Dragonfly returns EFAULT.
84+
switch {
85+
case err == nil:
86+
case errors.Is(err, syscall.ENAMETOOLONG):
87+
case runtime.GOOS == "dragonfly" && errors.Is(err, syscall.EFAULT):
88+
default:
7689
t.Fatalf("unexpected Stat error: %v", err)
7790
}
7891
break

0 commit comments

Comments
 (0)