Skip to content

Commit

Permalink
path/path: Avoid removing wildcard in front of /..
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed May 31, 2020
1 parent e8f5a33 commit db4c611
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/path/filepath/path.go
Expand Up @@ -124,10 +124,17 @@ func Clean(path string) string {
r += 2
switch {
case out.w > dotdot:
// can backtrack
out.w--
for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) {
// can backtrack, the first '3' in index means the length of '/..'
if out.path[r-3-3] == '.' && out.path[r-3-2] == '.' && out.path[r-3-1] == '.' {
out.append(Separator)
out.append('.')
out.append('.')
dotdot = out.w
} else {
out.w--
for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) {
out.w--
}
}
case !rooted:
// cannot backtrack, but not rooted, so append .. element.
Expand Down
1 change: 1 addition & 0 deletions src/path/filepath/path_test.go
Expand Up @@ -34,6 +34,7 @@ var cleantests = []PathTest{
{"../../abc", "../../abc"},
{"/abc", "/abc"},
{"/", "/"},
{"abc/def/.../..", "abc/def/.../.."},

// Empty is current dir
{"", "."},
Expand Down
11 changes: 9 additions & 2 deletions src/path/path.go
Expand Up @@ -105,9 +105,16 @@ func Clean(path string) string {
switch {
case out.w > dotdot:
// can backtrack
out.w--
for out.w > dotdot && out.index(out.w) != '/' {
if out.s[r-3-3] == '.' && out.s[r-3-2] == '.' && out.s[r-3-1] == '.' {
out.append('/')
out.append('.')
out.append('.')
dotdot = out.w
} else {
out.w--
for out.w > dotdot && out.index(out.w) != '/' {
out.w--
}
}
case !rooted:
// cannot backtrack, but not rooted, so append .. element.
Expand Down
1 change: 1 addition & 0 deletions src/path/path_test.go
Expand Up @@ -25,6 +25,7 @@ var cleantests = []PathTest{
{"../../abc", "../../abc"},
{"/abc", "/abc"},
{"/", "/"},
{"../../abc/.../..", "../../abc/.../.."},

// Remove trailing slash
{"abc/", "abc"},
Expand Down

0 comments on commit db4c611

Please sign in to comment.