peteraritchie / LongPath

drop-in library to support long paths in .NET
GNU Lesser General Public License v3.0
112 stars 43 forks source link

AccessViolationException in Directory.cs #26

Closed rootchord closed 8 years ago

rootchord commented 9 years ago

I wrote a simple program that reads folder permissions recursively for a large network share. Everything goes fine until it gets to a certain point then it throws an AccessViolationException pointing to this line:

var length = NativeMethods.GetSecurityDescriptorLength(byteArray);

Any Ideas?

peteraritchie commented 9 years ago

What is the detail of the exception? Usually that tells you exactly why it occurred.

rootchord commented 9 years ago

Here is the detail:

System.AccessViolationException was unhandled HResult=-2147467261 Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source=FRAC StackTrace: at Pri.LongPath.NativeMethods.GetSecurityDescriptorLength(IntPtr byteArray) at Pri.LongPath.Directory.GetAccessControl(String path, AccessControlSections includeSections) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\LongPath\Directory.cs:line 914 at Pri.LongPath.Directory.GetAccessControl(String path) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\LongPath\Directory.cs:line 868 at Pri.LongPath.DirectoryInfo.GetAccessControl() in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\LongPath\DirectoryInfo.cs:line 208 at FRAC.FRACMainForm.CheckAccess(DirectoryInfo directory) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 156 at FRAC.FRACMainForm.ProcessFolder(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 68 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 131 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.ProcessFolderRecursively(DirectoryInfo root) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 141 at FRAC.FRACMainForm.StartButton_Click(Object sender, EventArgs e) in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Form1.cs:line 217 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at FRAC.Program.Main() in REDACTED\Documents\Visual Studio 2010\Projects\FRAC\FRAC\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

rootchord commented 9 years ago

Sorry, accidentally closed it.

Any Ideas?

rootchord commented 9 years ago

Still having the same issue, any guidance?

peteraritchie commented 9 years ago

Unfortunately I'm unable to reproduce this so it's hard to do the right thing when it happens. Is there any other detail you can provide, like type of file system being accessed (local, share, soft-link, etc.)? Is it within a domain (security policies that get pushed, etc.). What type of security is on the directories being scanned?

rootchord commented 9 years ago

Well upon further investigation it looks like it's related to the issue with UNC paths. The error happens as soon as the UNC path exceeds 250 characters. If this sounds right, i'll close this issue.

jbolduan commented 9 years ago

I'm seeing the same behavior and I'm fairly certain it only happens when the UNC path exceeds 250 characters. Is there a good way around this or a fix?

Edit: I should say I get the same error message on File.GetAccessRule(\\server\share)

peteraritchie commented 8 years ago

Please try latest code with UNC and reparse point support. I cannot reproduce this with the latest build.