Merge pull request #4630 from kolyshkin/clean-path

libc/utils: simplify CleanPath
This commit is contained in:
Rodrigo Campos
2025-02-13 13:59:23 -03:00
committed by GitHub

View File

@@ -50,19 +50,19 @@ func CleanPath(path string) string {
// Ensure that all paths are cleaned (especially problematic ones like // Ensure that all paths are cleaned (especially problematic ones like
// "/../../../../../" which can cause lots of issues). // "/../../../../../" 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 // If the path isn't absolute, we need to do more processing to fix paths
// such as "../../../../<etc>/some/path". We also shouldn't convert absolute // such as "../../../../<etc>/some/path". We also shouldn't convert absolute
// paths to relative ones. // paths to relative ones.
if !filepath.IsAbs(path) { path = filepath.Clean(string(os.PathSeparator) + path)
path = filepath.Clean(string(os.PathSeparator) + path) // This can't fail, as (by definition) all paths are relative to root.
// This can't fail, as (by definition) all paths are relative to root. path, _ = filepath.Rel(string(os.PathSeparator), path)
path, _ = filepath.Rel(string(os.PathSeparator), path)
}
// Clean the path again for good measure. return path
return filepath.Clean(path)
} }
// stripRoot returns the passed path, stripping the root path if it was // stripRoot returns the passed path, stripping the root path if it was