1
0
mirror of https://github.com/golang/go synced 2024-11-11 17:51:49 -07:00

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 <iant@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Ian Lance Taylor 2024-09-03 15:13:01 -07:00 committed by Gopher Robot
parent a00195d304
commit af86efbe6d
4 changed files with 23 additions and 5 deletions

View File

@ -10,5 +10,8 @@ import "syscall"
type syscallErrorType = syscall.Errno
const errENOSYS = syscall.ENOSYS
const errERANGE = syscall.ERANGE
const (
errENOSYS = syscall.ENOSYS
errERANGE = syscall.ERANGE
errENOMEM = syscall.ENOMEM
)

View File

@ -10,3 +10,4 @@ type syscallErrorType = syscall.ErrorString
var errENOSYS = syscall.NewError("function not implemented")
var errERANGE = syscall.NewError("out of range")
var errENOMEM = syscall.NewError("cannot allocate memory")

View File

@ -60,9 +60,10 @@ func Getwd() (dir string, err error) {
}
}
// Linux returns ENAMETOOLONG if the result is too long.
// BSD systems appear to return EINVAL.
// Some BSD systems appear to return EINVAL.
// FreeBSD systems appear to use ENOMEM
// Solaris appears to use ERANGE.
if err != syscall.ENAMETOOLONG && err != syscall.EINVAL && err != errERANGE {
if err != syscall.ENAMETOOLONG && err != syscall.EINVAL && err != errERANGE && err != errENOMEM {
return dir, NewSyscallError("getwd", err)
}
}

View File

@ -9,6 +9,7 @@ package os_test
import (
"errors"
. "os"
"runtime"
"strings"
"syscall"
"testing"
@ -56,6 +57,12 @@ func testGetwdDeep(t *testing.T, setPWD bool) {
wd, err := Getwd()
t.Logf("Getwd len: %d", len(wd))
if err != nil {
// We can get an EPERM error if we can't read up
// to root, which happens on the Android builders.
if errors.Is(err, syscall.EPERM) {
t.Logf("ignoring EPERM error: %v", err)
break
}
t.Fatal(err)
}
if setPWD && wd != dir {
@ -72,7 +79,13 @@ func testGetwdDeep(t *testing.T, setPWD bool) {
// all Unix platforms (4096, on Linux).
if _, err := Stat(wd); err != nil || len(wd) > 4096 {
t.Logf("Done; len(wd)=%d", len(wd))
if err != nil && !errors.Is(err, syscall.ENAMETOOLONG) {
// Most systems return ENAMETOOLONG.
// Dragonfly returns EFAULT.
switch {
case err == nil:
case errors.Is(err, syscall.ENAMETOOLONG):
case runtime.GOOS == "dragonfly" && errors.Is(err, syscall.EFAULT):
default:
t.Fatalf("unexpected Stat error: %v", err)
}
break