Skip to content

Commit

Permalink
ARTOSReachability: don't crash on null instance lookup.
Browse files Browse the repository at this point in the history
See #593.
  • Loading branch information
tcard committed Apr 18, 2017
1 parent 50f0042 commit bada06b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Source/ARTOSReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,28 @@ @implementation ARTOSReachability {
NSString *_host;
void (^_callback)(BOOL);
SCNetworkReachabilityRef _reachabilityRef;
NSMutableDictionary *_instances;
}

- (instancetype)initWithLogger:(ARTLog *)logger {
self = [super self];
if (self) {
_logger = logger;
if (ARTOSReachability_instances == nil) {
_instances = [[NSMutableDictionary alloc] init];
ARTOSReachability_instances = _instances;
} else {
_instances = ARTOSReachability_instances;
ARTOSReachability_instances = [[NSMutableDictionary alloc] init];
}
}
return self;
}

__weak NSMutableDictionary *ARTOSReachability_instances;
NSMutableDictionary *ARTOSReachability_instances;

static void ARTOSReachability_Callback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
[(ARTOSReachability *)ARTOSReachability_instances[[NSValue valueWithPointer:target]] internalCallback:flags & kSCNetworkReachabilityFlagsReachable];
id instance = ARTOSReachability_instances[[NSValue valueWithPointer:target]];
if (instance == nil) {
NSLog(@"ARTOSReachability: instance not found for target %@", [NSValue valueWithPointer:target]);
return;
}
[(ARTOSReachability *)instance internalCallback:flags & kSCNetworkReachabilityFlagsReachable];
}

- (void)listenForHost:(NSString *)host callback:(void (^)(BOOL))callback {
Expand All @@ -52,13 +53,15 @@ - (void)listenForHost:(NSString *)host callback:(void (^)(BOOL))callback {
_reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [host UTF8String]);
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};

[ARTOSReachability_instances setObject:self forKey:[NSValue valueWithPointer:_reachabilityRef]];
if (SCNetworkReachabilitySetCallback(_reachabilityRef, ARTOSReachability_Callback, &context)) {
[ARTOSReachability_instances setObject:self forKey:[NSValue valueWithPointer:_reachabilityRef]];
if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) {
[_logger info:@"Reachability: started listening for host %@", _host];
} else {
[_logger warn:@"Reachability: failed starting listener for host %@", _host];
}
} else {
[ARTOSReachability_instances removeObjectForKey:[NSValue valueWithPointer:_reachabilityRef]];
}
}

Expand Down

0 comments on commit bada06b

Please sign in to comment.