Change exception throwing in Path.GetRelativePath (#25047)
authorRoman Marusyk <Marusyk@users.noreply.github.com>
Sun, 23 Jun 2019 02:55:17 +0000 (05:55 +0300)
committerJan Kotas <jkotas@microsoft.com>
Sun, 23 Jun 2019 02:55:17 +0000 (19:55 -0700)
* Fix Path.GetRelativePath throws ArgumentNullException when relativeTo is empty string

* Fix ArgumentException for relativeTo

src/System.Private.CoreLib/shared/System/IO/Path.cs

index 7412eb6..c581ee6 100644 (file)
@@ -812,8 +812,18 @@ namespace System.IO
 
         private static string GetRelativePath(string relativeTo, string path, StringComparison comparisonType)
         {
-            if (string.IsNullOrEmpty(relativeTo)) throw new ArgumentNullException(nameof(relativeTo));
-            if (PathInternal.IsEffectivelyEmpty(path.AsSpan())) throw new ArgumentNullException(nameof(path));
+            if (relativeTo == null)
+                throw new ArgumentNullException(nameof(relativeTo));
+
+            if (PathInternal.IsEffectivelyEmpty(relativeTo.AsSpan()))
+                throw new ArgumentException(SR.Arg_PathEmpty, nameof(relativeTo));
+
+            if (path == null)
+                throw new ArgumentNullException(nameof(path));
+
+            if (PathInternal.IsEffectivelyEmpty(path.AsSpan()))
+                throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
+
             Debug.Assert(comparisonType == StringComparison.Ordinal || comparisonType == StringComparison.OrdinalIgnoreCase);
 
             relativeTo = GetFullPath(relativeTo);