Skip to content

Commit 8db6ffb

Browse files
committed
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 <[email protected]>
1 parent 35a28ad commit 8db6ffb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

libcontainer/utils/utils.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func CleanPath(path string) string {
5050

5151
// Ensure that all paths are cleaned (especially problematic ones like
5252
// "/../../../../../" which can cause lots of issues).
53-
path = filepath.Clean(path)
53+
54+
if filepath.IsAbs(path) {
55+
return filepath.Clean(path)
56+
}
5457

5558
// If the path isn't absolute, we need to do more processing to fix paths
5659
// such as "../../../../<etc>/some/path". We also shouldn't convert absolute
5760
// paths to relative ones.
58-
if !filepath.IsAbs(path) {
59-
path = filepath.Clean(string(os.PathSeparator) + path)
60-
// This can't fail, as (by definition) all paths are relative to root.
61-
path, _ = filepath.Rel(string(os.PathSeparator), path)
62-
}
61+
path = filepath.Clean(string(os.PathSeparator) + path)
62+
// This can't fail, as (by definition) all paths are relative to root.
63+
path, _ = filepath.Rel(string(os.PathSeparator), path)
6364

64-
// Clean the path again for good measure.
65-
return filepath.Clean(path)
65+
return path
6666
}
6767

6868
// stripRoot returns the passed path, stripping the root path if it was

0 commit comments

Comments
 (0)