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

OCMock: Fix ios 15 device crash #14

Merged
merged 1 commit into from
May 4, 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
6 changes: 3 additions & 3 deletions Source/OCMock/OCMArg.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ + (void *)anyPointer

+ (id __autoreleasing *)anyObjectRef
{
return (id *)0x01234567;
return (id *)[self anyPointer];
}

+ (SEL)anySelector
Expand Down Expand Up @@ -127,9 +127,9 @@ + (id)resolveSpecialValues:(NSValue *)value
if(type[0] == '^')
{
void *pointer = [value pointerValue];
if(pointer == (void *)0x01234567)
if(pointer == [self anyPointer])
return [OCMArg any];
if((pointer != NULL) && (object_getClass((id)pointer) == [OCMPassByRefSetter class]))
if((pointer != NULL) && [OCMPassByRefSetter ptrIsPassByRefSetter:pointer])
return (id)pointer;
}
else if(type[0] == ':')
Expand Down
3 changes: 3 additions & 0 deletions Source/OCMock/OCMPassByRefSetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@

- (id)initWithValue:(id)value;

// Returns YES if ptr is actually a OCMPassByRefSetter
+ (BOOL)ptrIsPassByRefSetter:(void*)ptr;

@end
32 changes: 32 additions & 0 deletions Source/OCMock/OCMPassByRefSetter.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,30 @@

@implementation OCMPassByRefSetter

// Stores a reference to each of our OCMPassByRefSetters so that OCMArg can
// check any given pointer to verify that it is an OCMPassByRefSetter.
// The pointers are stored as naked pointers with no reference counts.
// Note: all accesses protected by @synchronized(gPointerTable)
static NSHashTable *gPointerTable = NULL;

+ (void)initialize
{
if (self == [OCMPassByRefSetter class])
{
gPointerTable = [[NSHashTable hashTableWithOptions:NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality] retain];
}
}

- (id)initWithValue:(id)aValue
{
if((self = [super init]))
{
value = [aValue retain];
@synchronized(gPointerTable)
{
// This will throw if somehow we manage to put two of the same pointer in the table.
NSHashInsertKnownAbsent(gPointerTable, self);
}
}

return self;
Expand All @@ -32,6 +51,11 @@ - (id)initWithValue:(id)aValue
- (void)dealloc
{
[value release];
@synchronized(gPointerTable)
{
NSAssert(NSHashGet(gPointerTable, self) != NULL, @"self should be in the hash table");
NSHashRemove(gPointerTable, self);
}
[super dealloc];
}

Expand All @@ -47,4 +71,12 @@ - (void)handleArgument:(id)arg
}
}

+ (BOOL)ptrIsPassByRefSetter:(void*)ptr
{
@synchronized(gPointerTable)
{
return NSHashGet(gPointerTable, ptr) != NULL;
}
}

@end