Bug 222634 - Use adoptNS() right away after calling [obj copy] / [obj mutableCopy]
Summary: Use adoptNS() right away after calling [obj copy] / [obj mutableCopy]
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-03-02 16:22 PST by Chris Dumez
Modified: 2021-03-03 15:08 PST (History)
7 users (show)

See Also:


Attachments
Patch (21.56 KB, patch)
2021-03-02 16:29 PST, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (21.55 KB, patch)
2021-03-03 13:15 PST, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2021-03-02 16:22:13 PST
Use adoptNS() right away after calling [obj copy] / [obj mutableCopy] to minimize the chance of leaks.
Comment 1 Chris Dumez 2021-03-02 16:29:07 PST
Created attachment 422020 [details]
Patch
Comment 2 Darin Adler 2021-03-03 13:02:18 PST
Comment on attachment 422020 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=422020&action=review

> Source/WebCore/platform/cocoa/SystemVersion.mm:40
> -    return [[systemVersionInfo objectForKey:@"ProductVersion"] copy];
> +    return [systemVersionInfo objectForKey:@"ProductVersion"];

What’s the rationale for moving from copy to retain here?

I’m not saying it’s wrong, but we are removing the copy. That means if it happens to be a mutable dictionary we will keep a pointer to it rather than making an immutable copy. I could imagine making the copy in an effort to avoid things being mutated under us or perhaps to optimize memory use since the immutable copy could be smaller.

> Source/WebKit/Shared/ApplePay/ApplePayPaymentSetupFeatures.mm:74
> +        allowedClasses.get() = WTFMove(allowed);

What’s the rationale for moving from copy to retain here?

> Source/WebKit/Shared/ApplePay/PaymentSetupConfiguration.mm:93
> +        allowedClasses.get() = WTFMove(allowed);

What’s the rationale for moving from copy to retain here?

> Source/WebKit/Shared/ApplePay/cocoa/PaymentSetupConfiguration.mm:70
> +        allowedClasses.get() = WTFMove(allowed);

What’s the rationale for moving from copy to retain here?

> Source/WebKit/UIProcess/Launcher/mac/ProcessLauncherMac.mm:110
> -    }() copy];
> +    }());

What’s the rationale for moving from copy to retain here?
Comment 3 Chris Dumez 2021-03-03 13:03:50 PST
(In reply to Darin Adler from comment #2)
> Comment on attachment 422020 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=422020&action=review
> 
> > Source/WebCore/platform/cocoa/SystemVersion.mm:40
> > -    return [[systemVersionInfo objectForKey:@"ProductVersion"] copy];
> > +    return [systemVersionInfo objectForKey:@"ProductVersion"];
> 
> What’s the rationale for moving from copy to retain here?
> 
> I’m not saying it’s wrong, but we are removing the copy. That means if it
> happens to be a mutable dictionary we will keep a pointer to it rather than
> making an immutable copy. I could imagine making the copy in an effort to
> avoid things being mutated under us or perhaps to optimize memory use since
> the immutable copy could be smaller.

I did not realize the benefits of calling [obj copy] here. I will re-introduce those then.

> 
> > Source/WebKit/Shared/ApplePay/ApplePayPaymentSetupFeatures.mm:74
> > +        allowedClasses.get() = WTFMove(allowed);
> 
> What’s the rationale for moving from copy to retain here?
> 
> > Source/WebKit/Shared/ApplePay/PaymentSetupConfiguration.mm:93
> > +        allowedClasses.get() = WTFMove(allowed);
> 
> What’s the rationale for moving from copy to retain here?
> 
> > Source/WebKit/Shared/ApplePay/cocoa/PaymentSetupConfiguration.mm:70
> > +        allowedClasses.get() = WTFMove(allowed);
> 
> What’s the rationale for moving from copy to retain here?
> 
> > Source/WebKit/UIProcess/Launcher/mac/ProcessLauncherMac.mm:110
> > -    }() copy];
> > +    }());
> 
> What’s the rationale for moving from copy to retain here?
Comment 4 Darin Adler 2021-03-03 13:05:52 PST
Comment on attachment 422020 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=422020&action=review

>>> Source/WebCore/platform/cocoa/SystemVersion.mm:40
>>> +    return [systemVersionInfo objectForKey:@"ProductVersion"];
>> 
>> What’s the rationale for moving from copy to retain here?
>> 
>> I’m not saying it’s wrong, but we are removing the copy. That means if it happens to be a mutable dictionary we will keep a pointer to it rather than making an immutable copy. I could imagine making the copy in an effort to avoid things being mutated under us or perhaps to optimize memory use since the immutable copy could be smaller.
> 
> I did not realize the benefits of calling [obj copy] here. I will re-introduce those then.

These are *possible* benefits. Also possible that someone had a choice of typing "retain" or "copy" and selected copy without thinking deeply about tradeoffs.
Comment 5 Chris Dumez 2021-03-03 13:15:57 PST
Created attachment 422142 [details]
Patch
Comment 6 EWS 2021-03-03 15:07:13 PST
Committed r273848: <https://commits.webkit.org/r273848>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 422142 [details].
Comment 7 Radar WebKit Bug Importer 2021-03-03 15:08:16 PST
<rdar://problem/75005651>