Skip to content

Instantly share code, notes, and snippets.

@rfht
Created June 25, 2019 16:58
Show Gist options
  • Save rfht/94830751fc5e6d6c4c9b19de8ccec848 to your computer and use it in GitHub Desktop.
Save rfht/94830751fc5e6d6c4c9b19de8ccec848 to your computer and use it in GitHub Desktop.
Directory.GetFiles() bug demo
using System;
using System.IO;
public class GetFilesBugDemo
{
public static void Main(string[] args)
{
Directory.CreateDirectory("test/test");
File.Create("test/test/testfile");
Console.WriteLine("Test with rooted path:");
foreach(var file in Directory.GetFiles(Directory.GetCurrentDirectory() + "/test//test//"))
{
Console.WriteLine(file.ToString());
}
Console.WriteLine("Test with relative path:");
foreach(var file in Directory.GetFiles("test//test//"))
{
Console.WriteLine(file.ToString());
}
}
}
@rfht
Copy link
Author

rfht commented Jun 25, 2019

On Windows 10:

C:\temp>mono --version
Mono JIT compiler version 5.20.1 (Visual Studio built mono)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       normal
        Notification:  Thread + polling
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        Interpreter:   yes
        LLVM:          supported, not enabled.
        Suspend:       preemptive
        GC:            sgen (concurrent by default)

C:\temp>mono GetFilesBugDemo.exe
Test with rooted path:
C:\temp/test//test//testfile
Test with relative path:
test//test//testfile

C:\temp>

@rfht
Copy link
Author

rfht commented Jun 25, 2019

On OpenBSD:

[email protected]:tmp$ mono --version
Mono JIT compiler version 5.20.1.19 (tarball Mon Jun 24 09:30:38 MDT 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           
	SIGSEGV:       normal
	Notification:  kqueue
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	Interpreter:   yes
	LLVM:          supported, not enabled.
	Suspend:       cooperative
	GC:            sgen (concurrent by default)

[email protected]:tmp$ mono ~/obsd/ports/mono/GetFilesBugDemo.exe
Test with rooted path:
/tmp/test//test//testfile
Test with relative path:

Unhandled Exception:
System.ArgumentException: Second path fragment must not be a drive or UNC name.
Parameter name: path2
  at System.IO.Path.InternalCombine (System.String path1, System.String path2) [0x00058] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () [0x00000] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[TSource] resultHandler, System.Boolean checkHost) [0x000d6] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) [0x00009] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) [0x00000] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.Directory.InternalGetFiles (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) [0x00000] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.Directory.GetFiles (System.String path) [0x0000e] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at GetFilesBugDemo.Main (System.String[] args) [0x00064] in <543e2be16ad84a57bf94fc0e7afafe1f>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Second path fragment must not be a drive or UNC name.
Parameter name: path2
  at System.IO.Path.InternalCombine (System.String path1, System.String path2) [0x00058] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () [0x00000] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[TSource] resultHandler, System.Boolean checkHost) [0x000d6] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) [0x00009] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) [0x00000] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.Directory.InternalGetFiles (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) [0x00000] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at System.IO.Directory.GetFiles (System.String path) [0x0000e] in <18bb096a455d486cb2c07ffa2b99294c>:0 
  at GetFilesBugDemo.Main (System.String[] args) [0x00064] in <543e2be16ad84a57bf94fc0e7afafe1f>:0 
[email protected]:tmp$

@rfht
Copy link
Author

rfht commented Jun 25, 2019

On Ubuntu:

thfr@thfr-MS-7B79:/tmp$ mono --version
Mono JIT compiler version 5.20.1.19 (tarball Thu Apr 11 09:02:17 UTC 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	Interpreter:   yes
	LLVM:          yes(600)
	Suspend:       hybrid
	GC:            sgen (concurrent by default)

thfr@thfr-MS-7B79:/tmp$ mono GetFilesBugDemo.exe
Test with rooted path:
/tmp/test//test//testfile
Test with relative path:

Unhandled Exception:
System.ArgumentException: Second path fragment must not be a drive or UNC name.
Parameter name: path2
  at System.IO.Path.InternalCombine (System.String path1, System.String path2) <0x7fda6deedb30 + 0x00165> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () <0x7fda6dec78f0 + 0x00046> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[TSource] resultHandler, System.Boolean checkHost) <0x7fda6dec7670 + 0x00270> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) <0x7fda6dec7210 + 0x00086> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) <0x7fda6dec4940 + 0x00050> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.Directory.InternalGetFiles (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) <0x7fda6dec44f0 + 0x00042> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.Directory.GetFiles (System.String path) <0x7fda6dec4390 + 0x0002b> in <6649516e5b3542319fb262b421af0adb>:0 
  at GetFilesBugDemo.Main (System.String[] args) [0x00064] in <6983a9c40c8442a29203ee2a26b43df5>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Second path fragment must not be a drive or UNC name.
Parameter name: path2
  at System.IO.Path.InternalCombine (System.String path1, System.String path2) <0x7fda6deedb30 + 0x00165> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource].CommonInit () <0x7fda6dec78f0 + 0x00046> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.FileSystemEnumerableIterator`1[TSource]..ctor (System.String path, System.String originalUserPath, System.String searchPattern, System.IO.SearchOption searchOption, System.IO.SearchResultHandler`1[TSource] resultHandler, System.Boolean checkHost) <0x7fda6dec7670 + 0x00270> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.FileSystemEnumerableFactory.CreateFileNameIterator (System.String path, System.String originalUserPath, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) <0x7fda6dec7210 + 0x00086> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.Directory.InternalGetFileDirectoryNames (System.String path, System.String userPathOriginal, System.String searchPattern, System.Boolean includeFiles, System.Boolean includeDirs, System.IO.SearchOption searchOption, System.Boolean checkHost) <0x7fda6dec4940 + 0x00050> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.Directory.InternalGetFiles (System.String path, System.String searchPattern, System.IO.SearchOption searchOption) <0x7fda6dec44f0 + 0x00042> in <6649516e5b3542319fb262b421af0adb>:0 
  at System.IO.Directory.GetFiles (System.String path) <0x7fda6dec4390 + 0x0002b> in <6649516e5b3542319fb262b421af0adb>:0 
  at GetFilesBugDemo.Main (System.String[] args) [0x00064] in <6983a9c40c8442a29203ee2a26b43df5>:0 

thfr@thfr-MS-7B79:/tmp$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment