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

Use guaranteed const for associated object key #141

Merged
merged 2 commits into from
May 22, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
`GULHeartbeatDateStorageUserDefaults` APIs. (#164)
- Remove '+ [GULAppEnvironmentUtil isIOS7OrHigher]' API. (#165)
- Remove '+ [GULAppEnvironmentUtil hasSwiftRuntime]' API. (#166)
- Fix an issue where ObjC associated objects were sometimes set with a
non-const key, potentially resulting in undefined behavior. (#141)

# 7.13.3
- Rename parameter placeholder in `GULSecureCoding` unarchiving API to avoid
Expand Down
2 changes: 1 addition & 1 deletion GoogleUtilities/ISASwizzler/GULObjectSwizzler+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#import "GoogleUtilities/ISASwizzler/Public/GoogleUtilities/GULObjectSwizzler.h"

FOUNDATION_EXPORT NSString *kGULSwizzlerAssociatedObjectKey;
FOUNDATION_EXPORT const NSString *const kGULSwizzlerAssociatedObjectKey;

@interface GULObjectSwizzler (Internal)

Expand Down
19 changes: 9 additions & 10 deletions GoogleUtilities/ISASwizzler/GULObjectSwizzler.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ @implementation GULObjectSwizzler {
#pragma mark - Class methods

+ (void)setAssociatedObject:(id)object
key:(NSString *)key
key:(const void *)key
value:(nullable id)value
association:(GUL_ASSOCIATION)association {
objc_AssociationPolicy resolvedAssociation;
Expand Down Expand Up @@ -61,11 +61,11 @@ + (void)setAssociatedObject:(id)object
default:
break;
}
objc_setAssociatedObject(object, key.UTF8String, value, resolvedAssociation);
objc_setAssociatedObject(object, key, value, resolvedAssociation);
}

+ (nullable id)getAssociatedObject:(id)object key:(NSString *)key {
return objc_getAssociatedObject(object, key.UTF8String);
+ (nullable id)getAssociatedObject:(id)object key:(const void *)key {
return objc_getAssociatedObject(object, key);
}

#pragma mark - Instance methods
Expand All @@ -81,7 +81,7 @@ - (instancetype)initWithObject:(id)object {
}

GULObjectSwizzler *existingSwizzler =
[[self class] getAssociatedObject:object key:kGULSwizzlerAssociatedObjectKey];
[[self class] getAssociatedObject:object key:&kGULSwizzlerAssociatedObjectKey];
if ([existingSwizzler isKindOfClass:[GULObjectSwizzler class]]) {
// The object has been swizzled already, no need to swizzle again.
return existingSwizzler;
Expand Down Expand Up @@ -110,7 +110,7 @@ - (void)copySelector:(SEL)selector fromClass:(Class)aClass isClassSelector:(BOOL
class_replaceMethod(targetClass, selector, implementation, typeEncoding);
}

- (void)setAssociatedObjectWithKey:(NSString *)key
- (void)setAssociatedObjectWithKey:(const void *)key
value:(id)value
association:(GUL_ASSOCIATION)association {
__strong id swizzledObject = _swizzledObject;
Expand All @@ -119,7 +119,7 @@ - (void)setAssociatedObjectWithKey:(NSString *)key
}
}

- (nullable id)getAssociatedObjectForKey:(NSString *)key {
- (nullable id)getAssociatedObjectForKey:(const void *)key {
__strong id swizzledObject = _swizzledObject;
if (swizzledObject) {
return [[self class] getAssociatedObject:swizzledObject key:key];
Expand All @@ -129,9 +129,8 @@ - (nullable id)getAssociatedObjectForKey:(NSString *)key {

- (void)swizzle {
__strong id swizzledObject = _swizzledObject;

GULObjectSwizzler *existingSwizzler =
[[self class] getAssociatedObject:swizzledObject key:kGULSwizzlerAssociatedObjectKey];
[[self class] getAssociatedObject:swizzledObject key:&kGULSwizzlerAssociatedObjectKey];
if (existingSwizzler != nil) {
NSAssert(existingSwizzler == self, @"The swizzled object has a different swizzler.");
// The object has been swizzled already.
Expand All @@ -140,7 +139,7 @@ - (void)swizzle {

if (swizzledObject) {
[GULObjectSwizzler setAssociatedObject:swizzledObject
key:kGULSwizzlerAssociatedObjectKey
key:&kGULSwizzlerAssociatedObjectKey
value:self
association:GUL_ASSOCIATION_RETAIN];

Expand Down
4 changes: 2 additions & 2 deletions GoogleUtilities/ISASwizzler/GULSwizzledObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#import "GoogleUtilities/ISASwizzler/GULObjectSwizzler+Internal.h"
#import "GoogleUtilities/ISASwizzler/Public/GoogleUtilities/GULSwizzledObject.h"

NSString *kGULSwizzlerAssociatedObjectKey = @"gul_objectSwizzler";
const NSString *const kGULSwizzlerAssociatedObjectKey = @"gul_objectSwizzler";
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved

@interface GULSwizzledObject ()

Expand Down Expand Up @@ -46,7 +46,7 @@ - (instancetype)init {
}

- (GULObjectSwizzler *)gul_objectSwizzler {
return [GULObjectSwizzler getAssociatedObject:self key:kGULSwizzlerAssociatedObjectKey];
return [GULObjectSwizzler getAssociatedObject:self key:&kGULSwizzlerAssociatedObjectKey];
}

#pragma mark - Donor methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef OBJC_ENUM(uintptr_t, GUL_ASSOCIATION){
* @param association The mechanism to use when associating the objects.
*/
+ (void)setAssociatedObject:(id)object
key:(NSString *)key
key:(const void *)key
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
value:(nullable id)value
association:(GUL_ASSOCIATION)association;

Expand All @@ -65,7 +65,7 @@ typedef OBJC_ENUM(uintptr_t, GUL_ASSOCIATION){
* @param object The object that will be queried for the associated object.
* @param key The key of the associated object.
*/
+ (nullable id)getAssociatedObject:(id)object key:(NSString *)key;
+ (nullable id)getAssociatedObject:(id)object key:(const void *)key;

/** Please use the designated initializer. */
- (instancetype)init NS_UNAVAILABLE;
Expand All @@ -88,7 +88,7 @@ typedef OBJC_ENUM(uintptr_t, GUL_ASSOCIATION){
* @param value The value to associate to the swizzled object.
* @param association The mechanism to use when associating the objects.
*/
- (void)setAssociatedObjectWithKey:(NSString *)key
- (void)setAssociatedObjectWithKey:(const void *)key
value:(id)value
association:(GUL_ASSOCIATION)association;

Expand All @@ -97,7 +97,7 @@ typedef OBJC_ENUM(uintptr_t, GUL_ASSOCIATION){
*
* @param key The key of the associated object.
*/
- (nullable id)getAssociatedObjectForKey:(NSString *)key;
- (nullable id)getAssociatedObjectForKey:(const void *)key;

/** Copies a selector from an existing class onto the generated dynamic subclass
* that this object will adopt. This mechanism can be used to add methods to
Expand Down
6 changes: 3 additions & 3 deletions GoogleUtilities/Tests/Unit/Swizzler/GULObjectSwizzlerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ - (void)testRespondsToSelectorWorksEvenIfSwizzledProxyISASwizzledBySomeoneElse {

// Release GULObjectSwizzler
[GULObjectSwizzler setAssociatedObject:proxyObject
key:kGULSwizzlerAssociatedObjectKey
key:&kGULSwizzlerAssociatedObjectKey
value:nil
association:GUL_ASSOCIATION_RETAIN];
}
Expand Down Expand Up @@ -395,7 +395,7 @@ - (void)testSwizzlerDoesntDisposeGeneratedClassWhenObjectIsISASwizzledBySomeoneE

// Release GULObjectSwizzler
[GULObjectSwizzler setAssociatedObject:object
key:kGULSwizzlerAssociatedObjectKey
key:&kGULSwizzlerAssociatedObjectKey
value:nil
association:GUL_ASSOCIATION_RETAIN];

Expand Down Expand Up @@ -439,7 +439,7 @@ - (void)disabledForCI_testSwizzlerDisposesGeneratedClass {

// Release GULObjectSwizzler
[GULObjectSwizzler setAssociatedObject:object
key:kGULSwizzlerAssociatedObjectKey
key:&kGULSwizzlerAssociatedObjectKey
value:nil
association:GUL_ASSOCIATION_RETAIN];

Expand Down
Loading