From 87bc53ae3c3a15c0579db3064b2001b68d26a6b1 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Fri, 3 May 2024 17:05:46 +1000 Subject: [PATCH] join: fix ELOOP error path 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: 322b9931ad83 ("Ditch pkg/errors, use native error (un)wrapping") Signed-off-by: Aleksa Sarai --- join.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/join.go b/join.go index 3080950..5ac23b9 100644 --- a/join.go +++ b/join.go @@ -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. @@ -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 = ""