Skip to content

Commit

Permalink
join: fix ELOOP error path
Browse files Browse the repository at this point in the history
When returning an error, we should use the original path in the error
message. Previously we would append the unresolved remaining path to the
root, which is not a real path.

Fixes: 322b993 ("Ditch pkg/errors, use native error (un)wrapping")
Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed May 3, 2024
1 parent e9be397 commit 87bc53a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions join.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ func SecureJoinVFS(root, unsafePath string, vfs VFS) (string, error) {

unsafePath = filepath.FromSlash(unsafePath)
var (
currentPath string
linksWalked int
currentPath string
remainingPath = unsafePath
linksWalked int
)
for unsafePath != "" {
if v := filepath.VolumeName(unsafePath); v != "" {
unsafePath = unsafePath[len(v):]
for remainingPath != "" {
if v := filepath.VolumeName(remainingPath); v != "" {
remainingPath = remainingPath[len(v):]
}

// Get the next path component.
var part string
if i := strings.IndexRune(unsafePath, filepath.Separator); i == -1 {
part, unsafePath = unsafePath, ""
if i := strings.IndexRune(remainingPath, filepath.Separator); i == -1 {
part, remainingPath = remainingPath, ""
} else {
part, unsafePath = unsafePath[:i], unsafePath[i+1:]
part, remainingPath = remainingPath[:i], remainingPath[i+1:]
}

// Apply the component lexically to the path we are building.
Expand Down Expand Up @@ -103,7 +104,7 @@ func SecureJoinVFS(root, unsafePath string, vfs VFS) (string, error) {
if err != nil {
return "", err
}
unsafePath = dest + string(filepath.Separator) + unsafePath
remainingPath = dest + string(filepath.Separator) + remainingPath
// Absolute symlinks reset any work we've already done.
if filepath.IsAbs(dest) {
currentPath = ""
Expand Down

0 comments on commit 87bc53a

Please sign in to comment.