Strange IndexOutOfRangeException in Net.Mime.MailBnfHelper.ReadToken
Hello,
When AutoUpdater.DownloadUpdate() executes, it downloads the zip file, and once it's done it throws this exception that is really strange, since I don't have any email stuff configured in the entire application and not sure how it gets to execute those code paths.
I'm using the library like it's presented in the readme and it worked fine, not sure when it started to fail like this. The failure is consistent and in both debug/release builds and on different machines with latest Windows 10 version 2004. The project is based on WPF and .net core 5 preview 6 - are there known issues with .net core 5? With 3.1 is certainly used to work, although the library itself uses .net framework so not sure it even matters.
Thanks
System.IndexOutOfRangeException HResult=0x80131508 Message=Index was outside the bounds of the array. Source=System.Net.Mail StackTrace:
System.Net.Mail.dll!System.Net.Mime.MailBnfHelper.ReadToken(string data, ref int offset, System.Text.StringBuilder builder) Line 278 C# System.Net.Mail.dll!System.Net.Mime.ContentDisposition.ParseValue() Line 268 C# System.Net.Mail.dll!System.Net.Mime.ContentDisposition.ContentDisposition(string disposition) Line 66 C# [External Code] System.Net.WebClient.dll!System.Net.WebClient.OnDownloadFileCompleted(System.ComponentModel.AsyncCompletedEventArgs e) Line 80 C# System.Net.WebClient.dll!System.Net.WebClient.StartAsyncOperation.AnonymousMethod__78_4(object arg) Line 118 C# WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 110 C# WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Line 36 C# WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() Line 540 C# WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state) Line 489 C# WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj) Line 215 C# System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 172 C# System.Private.CoreLib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 132 C# WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 172 C# WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() Line 412 C# WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() Line 2089 C# WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 2298 C# WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 295 C# WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Line 429 C# WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 103 C# WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Line 36 C# WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Line 1331 C# WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 341 C# [External Code] System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(System.UIntPtr dwComponentID, Interop.Mso.msoloop uReason, void* pvLoopData) Line 345 C# System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Interop.Mso.msoloop reason, System.Windows.Forms.ApplicationContext context) Line 1130 C# System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Interop.Mso.msoloop reason, System.Windows.Forms.ApplicationContext context) Line 992 C# System.Windows.Forms.dll!System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form form) Line 1201 C# System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window owner) Line 5293 C# System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() Line 5179 C#
It is caused by an internal exception while parsing the ContentDisposition header. You can try to replace the following code.
With this one.
Make sure you also copy the "TryToFindFileName" method from that file.
Thanks for the quick reply, this does work. The disposition string is empty, and the .net mail code seems to lack proper error handling and computes an invalid index in that string. Will a fix be made in the library to avoid this issue so that we don't need to build and replace the binary manually?
I prefer to use the method provided by the framework. That's why I replaced the old method with the one provided by the framework. I think I will wait until the stable .net core 5 version is released before making the change.