forked from dotnet/macios
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Foundation] Don't leak exceptions in WrappedNSInputStream.Read.
We don't want to leak exceptions back to the calling native code in WrappedNSInputStream.Read, because that will likely crash the process. Example stack trace: ObjectDisposed_StreamClosed (System.ObjectDisposedException) at System.ThrowHelper.ThrowObjectDisposedException_StreamClosed(String) + 0x3c at System.IO.MemoryStream.Read(Byte[], Int32, Int32) + 0x124 at System.Net.Http.MultipartContent.ContentReadStream.Read(Byte[], Int32, Int32) + 0x78 at System.Net.Http.NSUrlSessionHandler.WrappedNSInputStream.Read(IntPtr buffer, UIntPtr len) + 0x58 at MyApp!<BaseAddress>+0x7082f8 Instead return -1 from the Read method, which is documented as an error condition, and then also return a custom NSError from the Error property - which is also documented to be where the error is supposed to be surfaced. Ref: https://developer.apple.com/documentation/foundation/nsinputstream/1411544-read Ref: dotnet#20123.
- Loading branch information
1 parent
da2525b
commit 4262b24
Showing
3 changed files
with
58 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Runtime.Versioning; | ||
|
||
#nullable enable | ||
|
||
namespace Foundation { | ||
#if NET | ||
[SupportedOSPlatform ("ios")] | ||
[SupportedOSPlatform ("maccatalyst")] | ||
[SupportedOSPlatform ("macos")] | ||
[SupportedOSPlatform ("tvos")] | ||
#endif | ||
public class NSExceptionError : NSError { | ||
Exception exception; | ||
|
||
public Exception Exception { get => exception; } | ||
|
||
public NSExceptionError (Exception exception) | ||
: base ((NSString) exception.GetType ().FullName, exception.HResult, GetDictionary (exception)) | ||
{ | ||
this.exception = exception; | ||
} | ||
|
||
static NSDictionary GetDictionary (Exception e) | ||
{ | ||
var dict = new NSMutableDictionary (); | ||
dict [NSError.LocalizedDescriptionKey] = (NSString) e.Message; | ||
dict [NSError.LocalizedFailureReasonErrorKey] = (NSString) e.Message; | ||
return dict; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters