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

Dispose empty sets and try/catch exceptions #10955

Merged
merged 1 commit into from
Oct 27, 2022
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
20 changes: 15 additions & 5 deletions src/Core/src/Platform/iOS/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using Foundation;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -57,14 +58,23 @@ public static void CreatePlatformWindow(this IUIWindowSceneDelegate sceneDelegat
dicts.Add(session.UserInfo);
if (session.StateRestorationActivity?.UserInfo is not null)
dicts.Add(session.StateRestorationActivity.UserInfo);
if (connectionOptions.UserActivities is not null)
try
{
foreach (var u in connectionOptions.UserActivities)
using var activities = connectionOptions.UserActivities;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First-point-five level, make sure we also dispose the error set because there may be another set later on.

if (activities is not null)
{
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null)
dicts.Add(userActivity.UserInfo);
foreach (var u in activities)
{
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null)
dicts.Add(userActivity.UserInfo);
}
}
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
}
Comment on lines +73 to +77
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second-point-five protection, if we do somehow throw, then we know there is no user activity.


var window = CreatePlatformWindow(application, scene as UIWindowScene, dicts.ToArray());
if (window is not null)
Expand Down
30 changes: 24 additions & 6 deletions src/Essentials/src/Platform/WindowStateManager.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) =>
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
try
{
using var scenes = UIApplication.SharedApplication.ConnectedScenes;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First level of attack, make sure to dispose the "other" set so that there is no managed object around to get a miss-type.

var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
return null;
}
Comment on lines +110 to +115
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second level, if we do get an exception, we know it is because the set was empty, so do empty things.

}

// use the windows property (up to 13.0)
Expand All @@ -115,9 +124,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) =>
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
try
{
using var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
return null;
}
}

// use the windows property (up to 15.0)
Expand Down