Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 59f6987

Browse files
authored
WhiteSpace and PrefixParent subdirectory names throw exception (#27810)
1 parent 61aaf07 commit 59f6987

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ public DirectoryInfo CreateSubdirectory(string path)
5656
{
5757
if (path == null)
5858
throw new ArgumentNullException(nameof(path));
59-
if (path.Length == 0)
59+
if (PathInternal.IsEffectivelyEmpty(path))
6060
throw new ArgumentException(SR.Argument_PathEmpty, nameof(path));
6161
if (Path.IsPathRooted(path))
6262
throw new ArgumentException(SR.Arg_Path2IsRooted, nameof(path));
6363

6464
string fullPath = Path.GetFullPath(Path.Combine(FullPath, path));
6565

66-
if (0 != string.Compare(FullPath, 0, fullPath, 0, FullPath.Length, PathInternal.StringComparison))
66+
if (fullPath.Length < FullPath.Length
67+
|| (fullPath.Length > FullPath.Length && !PathInternal.IsDirectorySeparator(fullPath[FullPath.Length]))
68+
|| string.Compare(FullPath, 0, fullPath, 0, FullPath.Length, PathInternal.StringComparison) != 0)
6769
{
6870
throw new ArgumentException(SR.Format(SR.Argument_InvalidSubPath, path, FullPath), nameof(path));
6971
}

src/System.IO.FileSystem/tests/DirectoryInfo/CreateSubdirectory.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,16 @@ public void WindowsControlWhiteSpace_Core(string component)
159159

160160
[Theory,
161161
MemberData(nameof(SimpleWhiteSpace))]
162-
[PlatformSpecific(TestPlatforms.Windows)] // Simple whitespace is trimmed in path
162+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
163+
[PlatformSpecific(TestPlatforms.Windows)]
164+
public void WindowsSimpleWhiteSpaceThrowsException(string component)
165+
{
166+
Assert.Throws<ArgumentException>(() => new DirectoryInfo(TestDirectory).CreateSubdirectory(component));
167+
}
168+
169+
[Theory,
170+
MemberData(nameof(SimpleWhiteSpace))]
171+
[SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] // Simple whitespace is trimmed in path
163172
public void WindowsSimpleWhiteSpace(string component)
164173
{
165174
DirectoryInfo result = new DirectoryInfo(TestDirectory).CreateSubdirectory(component);
@@ -211,6 +220,16 @@ public void UNCPathWithOnlySlashes()
211220
Assert.Throws<ArgumentException>(() => testDir.CreateSubdirectory("//"));
212221
}
213222

223+
[Fact]
224+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
225+
public void ParentDirectoryNameAsPrefixShouldThrow()
226+
{
227+
string randomName = GetTestFileName();
228+
DirectoryInfo di = Directory.CreateDirectory(Path.Combine(TestDirectory, randomName));
229+
230+
Assert.Throws<ArgumentException>(() => di.CreateSubdirectory(Path.Combine("..", randomName + "abc", GetTestFileName())));
231+
}
232+
214233
#endregion
215234
}
216235
}

0 commit comments

Comments
 (0)