Browse Source

Fix macOS StorageProvider null pointer bugs (#21115)

* fix: correct null check variable in TryResolveFileReferenceUri on macOS

The null check on line 401 tested `fileUri` instead of `filePathUri`,
making it dead code since `fileUri` was already checked on line 394.
When `[fileUri filePathURL]` returned nil (non-file URL or unreachable
resource), the nil `filePathUri` was passed to `[filePathUri absoluteString]`
and then to `CreateAvnString`, causing a native crash.

* fix: initialize NSError to nil in SaveBookmarkToBytes on macOS

NSError* was declared without initialization, containing stack garbage.
On the success path (bookmarkData non-nil), Cocoa does not guarantee
zeroing the error out-parameter, so the subsequent `if (error != nil)`
check could read garbage and incorrectly call CreateAvnString with a
garbage pointer.

Initialize to nil and restructure to `else if` so the error is only
inspected when bookmarkData is nil (the failure path).

* chore: retrigger CI

* fix: guard out-parameters in SaveBookmarkToBytes

Initialize *ppv to nullptr on entry so callers never read garbage on
the nil-fileUri / nil-bookmarkData paths. Guard *err write with a
nullptr check for callers that pass no error out-parameter.

* chore: retrigger CI

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
release/12.0.1
Nathan Nguyen 2 months ago
committed by Julien Lebosquain
parent
commit
ff03d9559e
  1. 8
      native/Avalonia.Native/src/OSX/StorageProvider.mm

8
native/Avalonia.Native/src/OSX/StorageProvider.mm

@ -82,14 +82,16 @@ public:
if(ppv == nullptr)
return E_POINTER;
NSError* error;
*ppv = nullptr;
NSError* error = nil;
auto fileUri = [NSURL URLWithString: GetNSStringAndRelease(fileUriStr)];
auto bookmarkData = [fileUri bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
if (bookmarkData)
{
*ppv = CreateByteArray((void*)bookmarkData.bytes, (int)bookmarkData.length);
}
if (error != nil)
else if (error != nil && err != nullptr)
{
*err = CreateAvnString([error localizedDescription]);
}
@ -398,7 +400,7 @@ public:
}
auto filePathUri = [fileUri filePathURL];
if (fileUri == nil)
if (filePathUri == nil)
{
*ret = nullptr;
return S_OK;

Loading…
Cancel
Save