Skip to content

Commit adb665e

Browse files
JezzJezz
authored andcommitted
1 parent 616b8af commit adb665e

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ xcuserdata/
2121
*.moved-aside
2222
*.xccheckout
2323
*.xcscmblueprint
24+
.DS_Store
2425

2526
## Obj-C/Swift specific
2627
*.hmap

JJException.xcodeproj/xcuserdata/jezz.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
shouldBeEnabled = "Yes"
2828
ignoreCount = "0"
2929
continueAfterRunningActions = "No"
30-
filePath = "JJException/Source/MRC/NSObject+ZombieHook.m"
31-
timestampString = "572505624.197015"
30+
filePath = "JJException/ViewController.m"
31+
timestampString = "577100550.896796"
3232
startingColumnNumber = "9223372036854775807"
3333
endingColumnNumber = "9223372036854775807"
34-
startingLineNumber = "57"
35-
endingLineNumber = "57"
36-
landmarkName = "-hookDealloc"
34+
startingLineNumber = "73"
35+
endingLineNumber = "73"
36+
landmarkName = "-testArrayDictionaryUnrecognizedSelector"
3737
landmarkType = "7">
3838
</BreakpointContent>
3939
</BreakpointProxy>

JJException/Source/MRC/NSMutableArray+MutableArrayHook.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ + (void)jj_swizzleNSMutableArray{
2424
swizzleInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(insertObject:atIndex:), @selector(hookInsertObject:atIndex:));
2525
swizzleInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(removeObjectAtIndex:), @selector(hookRemoveObjectAtIndex:));
2626
swizzleInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(replaceObjectAtIndex:withObject:), @selector(hookReplaceObjectAtIndex:withObject:));
27+
swizzleInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(setObject:atIndexedSubscript:), @selector(hookSetObject:atIndexedSubscript:));
2728
swizzleInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(removeObjectsInRange:), @selector(hookRemoveObjectsInRange:));
2829

2930
swizzleInstanceMethod(NSClassFromString(@"__NSCFArray"), @selector(objectAtIndex:), @selector(hookObjectAtIndex:));
@@ -83,6 +84,14 @@ - (void) hookReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
8384
}
8485
}
8586

87+
- (void) hookSetObject:(id)object atIndexedSubscript:(NSUInteger)index {
88+
if (index < self.count && object) {
89+
[self hookSetObject:object atIndexedSubscript:index];
90+
}else{
91+
handleCrashException(JJExceptionGuardArrayContainer,[NSString stringWithFormat:@"NSMutableArray setObject invalid object:%@ atIndexedSubscript:%tu total:%tu",object,index,self.count]);
92+
}
93+
}
94+
8695
- (void) hookRemoveObjectsInRange:(NSRange)range {
8796
if (range.location + range.length <= self.count) {
8897
[self hookRemoveObjectsInRange:range];

JJException/Source/MRC/NSMutableDictionary+MutableDictionaryHook.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,31 @@ @implementation NSMutableDictionary (MutableDictionaryHook)
1818
+ (void)jj_swizzleNSMutableDictionary{
1919
swizzleInstanceMethod(NSClassFromString(@"__NSDictionaryM"), @selector(setObject:forKey:), @selector(hookSetObject:forKey:));
2020
swizzleInstanceMethod(NSClassFromString(@"__NSDictionaryM"), @selector(removeObjectForKey:), @selector(hookRemoveObjectForKey:));
21+
swizzleInstanceMethod(NSClassFromString(@"__NSDictionaryM"), @selector(setObject:forKeyedSubscript:), @selector(hookSetObject:forKeyedSubscript:));
2122
}
2223

2324
- (void) hookSetObject:(id)object forKey:(id)key {
2425
if (object && key) {
2526
[self hookSetObject:object forKey:key];
26-
}else{
27+
} else {
2728
handleCrashException(JJExceptionGuardDictionaryContainer,[NSString stringWithFormat:@"NSMutableDictionary setObject invalid object:%@ and key:%@",object,key],self);
2829
}
2930
}
3031

3132
- (void) hookRemoveObjectForKey:(id)key {
3233
if (key) {
3334
[self hookRemoveObjectForKey:key];
34-
}else{
35+
} else {
3536
handleCrashException(JJExceptionGuardDictionaryContainer,@"NSMutableDictionary removeObjectForKey nil key",self);
3637
}
3738
}
3839

40+
- (void)hookSetObject:(id)object forKeyedSubscript:(id<NSCopying>)key {
41+
if (object && key ) {
42+
[self hookSetObject:object forKeyedSubscript:key];
43+
} else {
44+
handleCrashException(JJExceptionGuardDictionaryContainer,[NSString stringWithFormat:@"NSMutableDictionary setObject object:%@ and forKeyedSubscript:%@",object,key],self);
45+
}
46+
}
47+
3948
@end

JJExceptionTests/JJExceptionTests.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ - (void)testMutableArrayException{
6868

6969
NSAssert([mutableArray objectAtIndex:2] == nil, @"Check invalid index crash");
7070
NSAssert(mutableArray[2] == nil, @"Check invalid index crash");
71+
72+
NSMutableArray* atIndexedSubscriptArray = @[@"text"].mutableCopy;
73+
atIndexedSubscriptArray[3] = @"iOS";
7174
}
7275

7376
- (void)testDictionaryException{
@@ -88,6 +91,10 @@ - (void)testMutableDictionaryException{
8891

8992
NSString* key = nil;
9093
[testDic removeObjectForKey:key];
94+
95+
NSMutableDictionary *forKeyedSubscript = @{@"name" : @"text"}.mutableCopy;
96+
NSString *subscript = nil;
97+
forKeyedSubscript[subscript] = @(25);
9198
}
9299

93100
- (void)testZombieException{

0 commit comments

Comments
 (0)