diff --git a/Source/NSHTTPCookieStorage.m b/Source/NSHTTPCookieStorage.m index 5e6a56e0b..46a2788fd 100644 --- a/Source/NSHTTPCookieStorage.m +++ b/Source/NSHTTPCookieStorage.m @@ -54,6 +54,11 @@ @implementation NSHTTPCookieStorage + (id) allocWithZone: (NSZone*)z { + /* This is intended to be a singleton, but having +allocWithZone: return + * the shared object does mean that code can call alloc followed by init + * on the existing object, so the -init method needs to check that it is + * safe by only altering the state of the object first time it is called. + */ return RETAIN([self sharedHTTPCookieStorage]); } @@ -78,21 +83,26 @@ + (NSHTTPCookieStorage *) sharedHTTPCookieStorage return storage; } -- init +- (id) init { - this->_policy = NSHTTPCookieAcceptPolicyAlways; - this->_cookies = [NSMutableArray new]; - [[NSDistributedNotificationCenter defaultCenter] - addObserver: self - selector: @selector(cookiesChangedNotification:) - name: NSHTTPCookieManagerCookiesChangedNotification - object: objectObserver]; - [[NSDistributedNotificationCenter defaultCenter] - addObserver: self - selector: @selector(acceptPolicyChangeNotification:) - name: NSHTTPCookieManagerAcceptPolicyChangedNotification - object: objectObserver]; - [self _updateFromCookieStore]; + /* Protect against someone re-initialising the shared store. + */ + if (nil == this->_cookies) + { + this->_cookies = [NSMutableArray new]; + this->_policy = NSHTTPCookieAcceptPolicyAlways; + [[NSDistributedNotificationCenter defaultCenter] + addObserver: self + selector: @selector(cookiesChangedNotification:) + name: NSHTTPCookieManagerCookiesChangedNotification + object: objectObserver]; + [[NSDistributedNotificationCenter defaultCenter] + addObserver: self + selector: @selector(acceptPolicyChangeNotification:) + name: NSHTTPCookieManagerAcceptPolicyChangedNotification + object: objectObserver]; + [self _updateFromCookieStore]; + } return self; } @@ -186,7 +196,7 @@ - (void) _updateFromCookieStore cookie = [NSHTTPCookie cookieWithProperties: props]; if (NO == [this->_cookies containsObject: cookie]) { - [this->_cookies addObject:cookie]; + [this->_cookies addObject: cookie]; } } }