Closed
Description
go version go1.12 darwin/amd64
and
go version devel +820ad17303 Fri Mar 1 18:26:37 2019 +0000 darwin/amd64
Relative paths with ../
and symlinks are not correctly evaluated.
This reproduces the issue on macOS, since the $TMPDIR
in/var
is a symlink by default, starting from the path $HOME/foo
func main() {
path := "../../../var/folders/"
fmt.Println(path)
fmt.Println(os.Getwd())
fmt.Println(filepath.EvalSymlinks(path))
fi, _ := os.Stat(path)
fmt.Println(fi.Name())
}
Which outputs
$ go run eval_symlinks.go
../../../var/folders/
/Users/jbardin/foo <nil>
lstat ../var: no such file or directory
folders
The resolution seems to depend on the number of ../
in the path, as it does work from $HOME
with ../../var/folder
. The errors strings also alternate the number of reported ../
depending on whether there were an odd or even number in the original path.
The behavior is the same on linux on linux with a similar directory structure.
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
bcmills commentedon Mar 1, 2019
Can you provide a reproducer that does not depend on a specific OS and working directory? I've read this issue three times and I still don't understand what exactly is wrong with the current logic.
[-]pah/filepath: EvalSymlinks, incorrect traversal of relative paths[/-][+]path/filepath: EvalSymlinks, incorrect traversal of relative paths[/+]bcmills commentedon Mar 1, 2019
CC @robpike @rsc for
path/filepath
ianlancetaylor commentedon Mar 1, 2019
If I'm reading this correctly, the case in question is when someone uses
..
to step up above the root directory.jbardin commentedon Mar 1, 2019
@ianlancetaylor, That's what I thought too (though it should still work in that case), but here it's using the correct number of
../
.Here's a self-contained repro for linux:
Dockerfile
eval_symlinks.go
ianlancetaylor commentedon Mar 1, 2019
I think this is a self-contained example that doesn't require Docker or assumptions about home directory layout.
ianlancetaylor commentedon Mar 1, 2019
When I run that program I see
In other words, evaluating the path fails by itself, and succeeds when adding a
../
prefix.gopherbot commentedon Mar 1, 2019
Change https://golang.org/cl/164762 mentions this issue:
path/filepath: handle extra .. in EvalSymlinks
jbardin commentedon Mar 1, 2019
@ianlancetaylor I don't think it's an extra
../
(I admit I haven't had time to walk through the code, so I don't know exactly what's going on). The examples I've managed to create all require a certain number of path segments to trigger.I modified your example to follow the patten above, which still fails with
164762
outputs:
ianlancetaylor commentedon Mar 1, 2019
Do you think you could add to the test in https://golang.org/cl/164762 to show the problem?
jbardin commentedon Mar 1, 2019
Sorry, here's the above example formatted as a test. If someone wants to remind me how to push a single commit to an existing changeset in gerrit, I can give it another try, but it's been a long time since I've used gerrit.
ianlancetaylor commentedon Mar 2, 2019
Thanks. I added a similar test case, and a fix for the problem.
10 remaining items