Skip to content

Commit bf821f6

Browse files
panjf2000gopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a187890 commit bf821f6

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/io/fs/sub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type SubFS interface {
3333
// chroot-style security mechanism, and Sub does not change that fact.
3434
func Sub(fsys FS, dir string) (FS, error) {
3535
if !ValidPath(dir) {
36-
return nil, &PathError{Op: "sub", Path: dir, Err: errors.New("invalid name")}
36+
return nil, &PathError{Op: "sub", Path: dir, Err: ErrInvalid}
3737
}
3838
if dir == "." {
3939
return fsys, nil
@@ -52,7 +52,7 @@ type subFS struct {
5252
// fullName maps name to the fully-qualified name dir/name.
5353
func (f *subFS) fullName(op string, name string) (string, error) {
5454
if !ValidPath(name) {
55-
return "", &PathError{Op: op, Path: name, Err: errors.New("invalid name")}
55+
return "", &PathError{Op: op, Path: name, Err: ErrInvalid}
5656
}
5757
return path.Join(f.dir, name), nil
5858
}

src/io/fs/sub_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fs_test
66

77
import (
8+
"errors"
89
. "io/fs"
910
"testing"
1011
)
@@ -54,4 +55,9 @@ func TestSub(t *testing.T) {
5455
if pe.Path != "nonexist" {
5556
t.Fatalf("Open(nonexist): err.Path = %q, want %q", pe.Path, "nonexist")
5657
}
58+
59+
_, err = sub.Open("./")
60+
if !errors.Is(err, ErrInvalid) {
61+
t.Fatalf("Open(./): error is %v, want %v", err, ErrInvalid)
62+
}
5763
}

0 commit comments

Comments
 (0)