dotnet/runtime

Directory.Delete(string, bool) throws StackOverflowException

Open

#84,344 建立於 2023年4月5日

在 GitHub 查看
 (16 留言) (0 反應) (1 負責人)C# (5,445 fork)batch import
area-System.IOhelp wantedin-pr

倉庫指標

Star
 (17,886 star)
PR 合併指標
 (平均合併 12天 11小時) (30 天內合併 661 個 PR)

描述

Description

Directory.Delete(string, bool) throws a StackOverflowException if the nesting depth of subdirectories is huge (in my case 14000).

Reproduction Steps

  • Create nested subdirectories like below:
    DirectoryInfo topMost = new DirectoryInfo("C:\\temp");
    int depth = 14000;

    DirectoryInfo subDir = topMost;
    for (int i = 0; i < depth; i++)
    {
        subDir = subDir.CreateSubdirectory("a");
    }
  • Then try to delete the top most directory
Directory.Delete(topMost.FullName, true);

Expected behavior

Topmost directory and its all subdirectories are deleted successfully.

Actual behavior

A StackOverflowException is thrown with the following stacktrace:

Stack overflow.
   at System.String.Concat(System.ReadOnlySpan`1<Char>, System.ReadOnlySpan`1<Char>, System.ReadOnlySpan`1<Char>)
   at System.IO.Path.JoinInternal(System.ReadOnlySpan`1<Char>, System.ReadOnlySpan`1<Char>)
   at System.IO.Path.CombineInternal(System.String, System.String)
   at System.IO.Path.Combine(System.String, System.String)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   at System.IO.FileSystem.RemoveDirectoryRecursive(System.String, WIN32_FIND_DATA ByRef, Boolean)
   ....
   ....

Regression?

No response

Known Workarounds

No response

Configuration

dotnet version: 6.0.301

operating system: Windows 10 version 21H2

Other information

No response

貢獻者指南