Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Foundation] Don't leak exceptions in WrappedNSInputStream.Read. #20131

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Foundation/NSExceptionError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Runtime.Versioning;

#nullable enable

namespace Foundation {
#if NET
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
#endif
public sealed 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;
IsDirectBinding = false;
}

static NSDictionary GetDictionary (Exception e)
{
var dict = new NSMutableDictionary ();
dict [NSError.LocalizedDescriptionKey] = (NSString) e.Message;
dict [NSError.LocalizedFailureReasonErrorKey] = (NSString) e.Message;
return dict;
}
}
}
36 changes: 25 additions & 11 deletions src/Foundation/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,7 @@ class WrappedNSInputStream : NSInputStream {
CFRunLoopSource source;
readonly Stream stream;
bool notifying;
NSError? error;

public WrappedNSInputStream (Stream inputStream)
{
Expand Down Expand Up @@ -1456,21 +1457,34 @@ public override void Close ()
[Preserve (Conditional = true)]
public override nint Read (IntPtr buffer, nuint len)
{
var sourceBytes = new byte [len];
var read = stream.Read (sourceBytes, 0, (int) len);
Marshal.Copy (sourceBytes, 0, buffer, (int) len);
try {
var sourceBytes = new byte [len];
var read = stream.Read (sourceBytes, 0, (int) len);
Marshal.Copy (sourceBytes, 0, buffer, (int) len);

if (notifying)
return read;
if (notifying)
return read;

notifying = true;
if (stream.CanSeek && stream.Position == stream.Length) {
Notify (CFStreamEventType.EndEncountered);
status = NSStreamStatus.AtEnd;
notifying = true;
if (stream.CanSeek && stream.Position == stream.Length) {
Notify (CFStreamEventType.EndEncountered);
status = NSStreamStatus.AtEnd;
}
notifying = false;

return read;
} catch (Exception e) {
// -1 means that the operation failed; more information about the error can be obtained with streamError.
error = new NSExceptionError (e);
return -1;
}
notifying = false;
}

return read;
[Preserve (Conditional = true)]
public override NSError Error {
get {
return error ?? base.Error;
}
}

[Preserve (Conditional = true)]
Expand Down
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ FOUNDATION_SOURCES = \
Foundation/NSDistributedNotificationCenter.cs \
Foundation/NSEnumerator_1.cs \
Foundation/NSErrorException.cs \
Foundation/NSExceptionError.cs \
Foundation/NSExpression.cs \
Foundation/NSFastEnumerationState.cs \
Foundation/NSFastEnumerator.cs \
Expand Down
5 changes: 5 additions & 0 deletions tests/introspection/ApiCtorInitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ public void DesignatedInitializer ()
if (!t.IsPublic || !NSObjectType.IsAssignableFrom (t))
continue;

// we only care about wrapper types (types with a native counterpart), and they all have a Register attribute.
var typeRegisterAttribute = t.GetCustomAttribute<RegisterAttribute> (false);
if (typeRegisterAttribute is null)
continue;

int designated = 0;
foreach (var ctor in t.GetConstructors ()) {
if (ctor.GetCustomAttribute<DesignatedInitializerAttribute> () is null)
Expand Down
Loading