Skip to content

Commit

Permalink
io/fs: set ErrInvalid for FS.Open from SubFS when it fails ValidPath
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
panjf2000 authored and gopherbot committed Feb 10, 2024
1 parent a187890 commit bf821f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/io/fs/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions src/io/fs/sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package fs_test

import (
"errors"
. "io/fs"
"testing"
)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit bf821f6

Please sign in to comment.