Skip to content

Commit

Permalink
Merge pull request #281 from iangithubusername/PassByReferenceCrash
Browse files Browse the repository at this point in the history
+[OCMArg setTo:] crashes if invoked with NULL
  • Loading branch information
erikdoe committed Jan 20, 2016
2 parents 7d5f199 + 867d7a5 commit 6689ba4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Source/OCMock/OCMPassByRefSetter.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ - (void)dealloc

- (void)handleArgument:(id)arg
{
if([value isKindOfClass:[NSValue class]])
[(NSValue *)value getValue:[arg pointerValue]];
else
*(id *)[arg pointerValue] = value;
void *pointerValue = [arg pointerValue];
if(pointerValue != NULL)
{
if([value isKindOfClass:[NSValue class]])
[(NSValue *)value getValue:pointerValue];
else
*(id *)pointerValue = value;
}
}

@end
28 changes: 28 additions & 0 deletions Source/OCMockTests/OCMockObjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ - (void)doStuffWithBlock:(void (^)())block andString:(id)aString;
@end


@interface TestClassWithByReferenceMethod : NSObject

- (void)returnValuesInObjectPointer:(id *)objectPointer booleanPointer:(BOOL *)booleanPointer;

@end

@implementation TestClassWithByReferenceMethod

- (void)returnValuesInObjectPointer:(id *)objectPointer booleanPointer:(BOOL *)booleanPointer
{
if(objectPointer != NULL)
*objectPointer = [[NSObject alloc] init];
if(booleanPointer != NULL)
*booleanPointer = NO;
}

@end


@interface NotificationRecorderForTesting : NSObject
{
@public
Expand Down Expand Up @@ -637,6 +656,15 @@ - (void)testReturnsValuesInNonObjectPassByReferenceArguments
}


- (void)testReturnsValuesInNullPassByReferenceArguments
{
mock = OCMClassMock([TestClassWithByReferenceMethod class]);
OCMStub([mock returnValuesInObjectPointer:[OCMArg setTo:nil] booleanPointer:[OCMArg setToValue:@NO]]);
[mock returnValuesInObjectPointer:NULL booleanPointer:NULL];
OCMVerify([mock returnValuesInObjectPointer:NULL booleanPointer:NULL]);
}


// --------------------------------------------------------------------------------------
// invoking block arguments
// --------------------------------------------------------------------------------------
Expand Down

0 comments on commit 6689ba4

Please sign in to comment.