Skip to content

Commit

Permalink
Fix potential leak is the shared object is initialised more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Jan 17, 2025
1 parent f48aca6 commit 3f7647f
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions Source/NSHTTPCookieStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -186,7 +196,7 @@ - (void) _updateFromCookieStore
cookie = [NSHTTPCookie cookieWithProperties: props];
if (NO == [this->_cookies containsObject: cookie])
{
[this->_cookies addObject:cookie];
[this->_cookies addObject: cookie];
}
}
}
Expand Down

0 comments on commit 3f7647f

Please sign in to comment.