From 8db6ffbeefe5e92aa213c498b48bcfb8c3d2972a Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 12 Feb 2025 19:56:00 -0800 Subject: [PATCH] libc/utils: simplify CleanPath This simplifies the code flow and basically removes the last filepath.Clean, which is not necessary in either case: - for absolute path, single filepath.Clean is enough (as it is guaranteed to remove all dot and dot-dot elements); - for relative path, filepath.Rel calls Clean at the end (which is even documented). Signed-off-by: Kir Kolyshkin --- libcontainer/utils/utils.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libcontainer/utils/utils.go b/libcontainer/utils/utils.go index db420ea68..42f2eb99f 100644 --- a/libcontainer/utils/utils.go +++ b/libcontainer/utils/utils.go @@ -50,19 +50,19 @@ func CleanPath(path string) string { // Ensure that all paths are cleaned (especially problematic ones like // "/../../../../../" which can cause lots of issues). - path = filepath.Clean(path) + + if filepath.IsAbs(path) { + return filepath.Clean(path) + } // If the path isn't absolute, we need to do more processing to fix paths // such as "../../../..//some/path". We also shouldn't convert absolute // paths to relative ones. - if !filepath.IsAbs(path) { - path = filepath.Clean(string(os.PathSeparator) + path) - // This can't fail, as (by definition) all paths are relative to root. - path, _ = filepath.Rel(string(os.PathSeparator), path) - } + path = filepath.Clean(string(os.PathSeparator) + path) + // This can't fail, as (by definition) all paths are relative to root. + path, _ = filepath.Rel(string(os.PathSeparator), path) - // Clean the path again for good measure. - return filepath.Clean(path) + return path } // stripRoot returns the passed path, stripping the root path if it was