Fix handling of generating relative path to parent (#18460)
authorJeremy Kuhne <jeremy.kuhne@microsoft.com>
Fri, 15 Jun 2018 03:12:28 +0000 (20:12 -0700)
committerJeremy Kuhne <jeremy.kuhne@microsoft.com>
Wed, 27 Jun 2018 00:06:57 +0000 (17:06 -0700)
Fixes #30263

src/mscorlib/shared/System/IO/Path.cs

index 1e40ab5..d92df5a 100644 (file)
@@ -731,13 +731,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("..");
                     }
                 }
             }
@@ -749,11 +750,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);
         }