Skip to content

Commit

Permalink
Merge pull request #14 from Lightricks/feature/fix-ios-15-device-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
byohay authored May 4, 2022
2 parents 610dc18 + 5c318da commit 0f2f5a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
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

0 comments on commit 0f2f5a8

Please sign in to comment.