diff --git a/ChangeLog b/ChangeLog index 6186ee5ba..1a49c8d4f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2012-01-25 Richard Frith-Macdonald + + * Source/GSSocketStream.m: fix possible nul pointer dereference. + * Source/NSHost.m: cache failed lookup by name so that repeated + lookups for a band host name do not cause repeated DNS queries. + 2012-01-24 Richard Frith-Macdonald * Source/NSURLRequest.m: be tolerant about missing URL on init. diff --git a/Source/GSSocketStream.m b/Source/GSSocketStream.m index a5a2b39da..d94196255 100644 --- a/Source/GSSocketStream.m +++ b/Source/GSSocketStream.m @@ -141,6 +141,10 @@ GSPrivateSockaddrSetup(NSString *machine, uint16_t port, n = [machine UTF8String]; } + if (0 == n) + { + return NO; + } if (0 == strchr(n, ':')) { struct sockaddr_in *addr = (struct sockaddr_in*)sin; diff --git a/Source/NSHost.m b/Source/NSHost.m index c7e9656cd..668fe96a2 100644 --- a/Source/NSHost.m +++ b/Source/NSHost.m @@ -34,6 +34,7 @@ #import "Foundation/NSArray.h" #import "Foundation/NSDictionary.h" #import "Foundation/NSEnumerator.h" +#import "Foundation/NSNull.h" #import "Foundation/NSSet.h" #import "Foundation/NSCoder.h" @@ -57,6 +58,7 @@ static Class hostClass; static NSRecursiveLock *_hostCacheLock = nil; static BOOL _hostCacheEnabled = YES; static NSMutableDictionary *_hostCache = nil; +static id null = nil; @interface NSHost (Private) @@ -75,7 +77,7 @@ static NSMutableDictionary *_hostCache = nil; [s addObject: name]; ASSIGNCOPY(_names, s); RELEASE(s); - if (_hostCacheEnabled == YES) + if (YES == _hostCacheEnabled) { [_hostCache setObject: self forKey: name]; } @@ -91,7 +93,7 @@ static NSMutableDictionary *_hostCache = nil; name = [name copy]; _names = [[NSSet alloc] initWithObjects: &name count: 1]; _addresses = RETAIN(_names); - if (_hostCacheEnabled == YES) + if (YES == _hostCacheEnabled) { [_hostCache setObject: self forKey: name]; } @@ -202,7 +204,7 @@ static NSMutableDictionary *_hostCache = nil; _addresses = [addresses copy]; RELEASE(addresses); - if (_hostCacheEnabled == YES) + if (YES == _hostCacheEnabled) { [_hostCache setObject: self forKey: name]; } @@ -261,6 +263,7 @@ myHostName() if (self == [NSHost class]) { hostClass = self; + null = [[NSNull null] retain]; _hostCacheLock = [[NSRecursiveLock alloc] init]; _hostCache = [NSMutableDictionary new]; } @@ -298,7 +301,7 @@ myHostName() } [_hostCacheLock lock]; - if (_hostCacheEnabled == YES) + if (YES == _hostCacheEnabled) { host = [_hostCache objectForKey: name]; } @@ -332,6 +335,10 @@ myHostName() } else { + if (YES == _hostCacheEnabled) + { + [_hostCache setObject: null forKey: name]; + } NSLog(@"Host '%@' not found using 'gethostbyname()' - " @"perhaps the hostname is wrong or networking is not " @"set up on your machine", name); @@ -344,6 +351,10 @@ myHostName() } } } + else if ((id)host == null) + { + host = nil; + } else { IF_NO_GC([[host retain] autorelease];) @@ -406,7 +417,7 @@ myHostName() #endif [_hostCacheLock lock]; - if (_hostCacheEnabled == YES) + if (YES == _hostCacheEnabled) { host = [_hostCache objectForKey: address]; }