diff options
| author | Andy Pan <panjf2000@gmail.com> | 2024-02-01 11:28:44 +0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-02-10 02:10:17 +0000 |
| commit | bf821f65cfd61dcc431922eea2cb97ce0825d60c (patch) | |
| tree | 913fe02779266998f2a196bd5767d0d76e3bc345 /src/io | |
| parent | a18789041cb1cbb80def6f3fd05a11d353c9088b (diff) | |
| download | go-bf821f65cfd61dcc431922eea2cb97ce0825d60c.tar.xz | |
io/fs: set ErrInvalid for FS.Open from SubFS when it fails ValidPath
Fixes #65419
Change-Id: I8f9f82ab0387d8bb39aaca4f9e60e36ee15c587d
Reviewed-on: https://go-review.googlesource.com/c/go/+/560137
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/fs/sub.go | 4 | ||||
| -rw-r--r-- | src/io/fs/sub_test.go | 6 |
2 files changed, 8 insertions, 2 deletions
diff --git a/src/io/fs/sub.go b/src/io/fs/sub.go index 9999e63b26..70ac623077 100644 --- a/src/io/fs/sub.go +++ b/src/io/fs/sub.go @@ -33,7 +33,7 @@ type SubFS interface { // chroot-style security mechanism, and Sub does not change that fact. func Sub(fsys FS, dir string) (FS, error) { if !ValidPath(dir) { - return nil, &PathError{Op: "sub", Path: dir, Err: errors.New("invalid name")} + return nil, &PathError{Op: "sub", Path: dir, Err: ErrInvalid} } if dir == "." { return fsys, nil @@ -52,7 +52,7 @@ type subFS struct { // fullName maps name to the fully-qualified name dir/name. func (f *subFS) fullName(op string, name string) (string, error) { if !ValidPath(name) { - return "", &PathError{Op: op, Path: name, Err: errors.New("invalid name")} + return "", &PathError{Op: op, Path: name, Err: ErrInvalid} } return path.Join(f.dir, name), nil } diff --git a/src/io/fs/sub_test.go b/src/io/fs/sub_test.go index 451b0efb02..c0bb2fd5b8 100644 --- a/src/io/fs/sub_test.go +++ b/src/io/fs/sub_test.go @@ -5,6 +5,7 @@ package fs_test import ( + "errors" . "io/fs" "testing" ) @@ -54,4 +55,9 @@ func TestSub(t *testing.T) { if pe.Path != "nonexist" { t.Fatalf("Open(nonexist): err.Path = %q, want %q", pe.Path, "nonexist") } + + _, err = sub.Open("./") + if !errors.Is(err, ErrInvalid) { + t.Fatalf("Open(./): error is %v, want %v", err, ErrInvalid) + } } |
