1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

FileChooser: Guard against use-after-free

This commit is contained in:
reuk 2025-03-17 23:43:08 +00:00
parent 5e44dc0b95
commit cd981c1b1a
No known key found for this signature in database

View file

@ -33,11 +33,11 @@
*/
@interface FileChooserControllerClass : UIDocumentPickerViewController
- (void) setParent: (FileChooser::Native*) ptr;
- (void) setParent: (std::shared_ptr<FileChooser::Native>) ptr;
@end
@interface FileChooserDelegateClass : NSObject<UIDocumentPickerDelegate, UIAdaptivePresentationControllerDelegate>
- (id) initWithOwner: (FileChooser::Native*) owner;
- (void) setParent: (std::shared_ptr<FileChooser::Native>) owner;
@end
namespace juce
@ -56,7 +56,8 @@ public:
Note that we can't call this directly inside the class constructor, because
the owning shared_ptr might not yet exist.
*/
[result->controller.get() setParent: result.get()];
[result->controller.get() setParent: result];
[result->delegate.get() setParent: result];
return result;
}
@ -247,7 +248,7 @@ private:
: owner (fileChooser),
savedFlags (flags)
{
delegate.reset ([[FileChooserDelegateClass alloc] initWithOwner: this]);
delegate.reset ([[FileChooserDelegateClass alloc] init]);
const auto validExtensions = getValidExtensionsForWildcards (owner.filters);
@ -355,7 +356,7 @@ private:
//==============================================================================
FileChooser& owner;
NSUniquePtr<NSObject<UIDocumentPickerDelegate, UIAdaptivePresentationControllerDelegate>> delegate;
NSUniquePtr<FileChooserDelegateClass> delegate;
NSUniquePtr<FileChooserControllerClass> controller;
int savedFlags = 0;
@ -386,43 +387,39 @@ std::shared_ptr<FileChooser::Pimpl> FileChooser::showPlatformDialog (FileChooser
std::weak_ptr<FileChooser::Native> ptr;
}
- (void) setParent: (FileChooser::Native*) parent
- (void) setParent: (std::shared_ptr<FileChooser::Native>) parent
{
jassert (parent != nullptr);
jassert (parent->shared_from_this() != nullptr);
ptr = parent->weak_from_this();
ptr = parent;
}
@end
@implementation FileChooserDelegateClass
{
FileChooser::Native* owner;
std::weak_ptr<FileChooser::Native> weak;
}
- (id) initWithOwner: (FileChooser::Native*) o
- (void) setParent: (std::shared_ptr<FileChooser::Native>) o
{
self = [super init];
owner = o;
return self;
weak = o;
}
- (void) documentPicker: (UIDocumentPickerViewController*) controller didPickDocumentsAtURLs: (NSArray<NSURL*>*) urls
{
if (owner != nullptr)
owner->didPickDocumentsAtURLs (urls);
if (auto strong = weak.lock())
strong->didPickDocumentsAtURLs (urls);
}
- (void) documentPickerWasCancelled: (UIDocumentPickerViewController*) controller
{
if (owner != nullptr)
owner->pickerWasCancelled();
if (auto strong = weak.lock())
strong->pickerWasCancelled();
}
- (void) presentationControllerDidDismiss: (UIPresentationController *) presentationController
{
if (owner != nullptr)
owner->pickerWasCancelled();
if (auto strong = weak.lock())
strong->pickerWasCancelled();
}
@end