Skip to content

Commit a4976b5

Browse files
committed
Fix WalkDir and Walk pass unclean path
1 parent 3b5eec9 commit a4976b5

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/path/filepath/path.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,12 @@ func walk(path string, info fs.FileInfo, walkFn WalkFunc) error {
462462
//
463463
// WalkDir does not follow symbolic links.
464464
func WalkDir(root string, fn fs.WalkDirFunc) error {
465-
info, err := os.Lstat(root)
465+
cleanRootPath := Clean(root)
466+
info, err := os.Lstat(cleanRootPath)
466467
if err != nil {
467-
err = fn(root, nil, err)
468+
err = fn(cleanRootPath, nil, err)
468469
} else {
469-
err = walkDir(root, &statDirEntry{info}, fn)
470+
err = walkDir(cleanRootPath, &statDirEntry{info}, fn)
470471
}
471472
if err == SkipDir {
472473
return nil
@@ -498,11 +499,12 @@ func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
498499
// Walk is less efficient than WalkDir, introduced in Go 1.16,
499500
// which avoids calling os.Lstat on every visited file or directory.
500501
func Walk(root string, fn WalkFunc) error {
501-
info, err := os.Lstat(root)
502+
cleanRootPath := Clean(root)
503+
info, err := os.Lstat(cleanRootPath)
502504
if err != nil {
503-
err = fn(root, nil, err)
505+
err = fn(cleanRootPath, nil, err)
504506
} else {
505-
err = walk(root, info, fn)
507+
err = walk(cleanRootPath, info, fn)
506508
}
507509
if err == SkipDir {
508510
return nil

0 commit comments

Comments
 (0)