From d08af0e8be84b0a65c46e94bc48be917e34b74cc Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Thu, 14 Jun 2018 20:12:28 -0700 Subject: [PATCH] Fix handling of generating relative path to parent (dotnet/coreclr#18460) Fixes dotnet/coreclr#30263 Commit migrated from https://github.com/dotnet/coreclr/commit/5bb221911996edeef0373ecf5c5fbfc235533ac8 --- .../System.Private.CoreLib/src/System/IO/Path.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs index ec9b1b3..e619ecd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs @@ -741,13 +741,14 @@ namespace System.IO // Add parent segments for segments past the common on the "from" path if (commonLength < relativeToLength) { - sb.Append(PathInternal.ParentDirectoryPrefix); + sb.Append(".."); - for (int i = commonLength; i < relativeToLength; i++) + for (int i = commonLength + 1; i < relativeToLength; i++) { if (PathInternal.IsDirectorySeparator(relativeTo[i])) { - sb.Append(PathInternal.ParentDirectoryPrefix); + sb.Append(DirectorySeparatorChar); + sb.Append(".."); } } } @@ -759,11 +760,20 @@ namespace System.IO } // Now add the rest of the "to" path, adding back the trailing separator - int count = pathLength - commonLength; + int differenceLength = pathLength - commonLength; if (pathEndsInSeparator) - count++; + differenceLength++; + + if (differenceLength > 0) + { + if (sb.Length > 0) + { + sb.Append(DirectorySeparatorChar); + } + + sb.Append(path, commonLength, differenceLength); + } - sb.Append(path, commonLength, count); return StringBuilderCache.GetStringAndRelease(sb); } -- 2.7.4