Bug 1342442 - Add null-check over gService in nsCacheService. r=michal, a=ritu
authorHonza Bambas <honzab.moz@firemni.cz>
Mon, 06 Mar 2017 05:46:00 -0500
changeset 395088 7dfb1fe34ea5be93b23e8a6c077948ee84955c74
parent 395087 a719817d7347c16974ca8cc1226afcc10ea6dee4
child 395089 a7744be94bea4581220cfce7c48e22fb96ac2bf3
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal, ritu
bugs1342442
milestone54.0a2
Bug 1342442 - Add null-check over gService in nsCacheService. r=michal, a=ritu
netwerk/cache/nsCacheService.cpp
--- a/netwerk/cache/nsCacheService.cpp
+++ b/netwerk/cache/nsCacheService.cpp
@@ -831,25 +831,25 @@ nsCacheProfilePrefObserver::ReadPrefs(ns
                                &mClearCacheOnShutdown);
 
     return rv;
 }
 
 nsresult
 nsCacheService::DispatchToCacheIOThread(nsIRunnable* event)
 {
-    if (!gService->mCacheIOThread) return NS_ERROR_NOT_AVAILABLE;
+    if (!gService || !gService->mCacheIOThread) return NS_ERROR_NOT_AVAILABLE;
     return gService->mCacheIOThread->Dispatch(event, NS_DISPATCH_NORMAL);
 }
 
 nsresult
 nsCacheService::SyncWithCacheIOThread()
 {
+    if (!gService || !gService->mCacheIOThread) return NS_ERROR_NOT_AVAILABLE;
     gService->mLock.AssertCurrentThreadOwns();
-    if (!gService->mCacheIOThread) return NS_ERROR_NOT_AVAILABLE;
 
     nsCOMPtr<nsIRunnable> event = new nsBlockOnCacheThreadEvent();
 
     // dispatch event - it will notify the monitor when it's done
     nsresult rv =
         gService->mCacheIOThread->Dispatch(event, NS_DISPATCH_NORMAL);
     if (NS_FAILED(rv)) {
         NS_WARNING("Failed dispatching block-event");
@@ -1424,19 +1424,17 @@ nsCacheService::IsStorageEnabledForPolic
 
 nsresult
 nsCacheService::DoomEntry(nsCacheSession   *session,
                           const nsACString &key,
                           nsICacheListener *listener)
 {
     CACHE_LOG_DEBUG(("Dooming entry for session %p, key %s\n",
                      session, PromiseFlatCString(key).get()));
-    NS_ASSERTION(gService, "nsCacheService::gService is null.");
-
-    if (!gService->mInitialized)
+    if (!gService || !gService->mInitialized)
         return NS_ERROR_NOT_INITIALIZED;
 
     return DispatchToCacheIOThread(new nsDoomEvent(session, key, listener));
 }
 
 
 bool          
 nsCacheService::IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy  storagePolicy)
@@ -1679,17 +1677,17 @@ public:
 
         return NS_OK;
     }
 };
 
 void
 nsCacheService::MarkStartingFresh()
 {
-    if (!gService->mObserver->ShouldUseOldMaxSmartSize()) {
+    if (!gService || !gService->mObserver->ShouldUseOldMaxSmartSize()) {
         // Already using new max, nothing to do here
         return;
     }
 
     gService->mObserver->SetUseNewMaxSmartSize(true);
 
     // We always dispatch an event here because we don't want to deal with lock
     // reentrance issues.
@@ -2022,21 +2020,20 @@ nsCacheService::OpenCacheEntry(nsCacheSe
                                nsCacheAccessMode          accessRequested,
                                bool                       blockingMode,
                                nsICacheListener *         listener,
                                nsICacheEntryDescriptor ** result)
 {
     CACHE_LOG_DEBUG(("Opening entry for session %p, key %s, mode %d, blocking %d\n",
                      session, PromiseFlatCString(key).get(), accessRequested,
                      blockingMode));
-    NS_ASSERTION(gService, "nsCacheService::gService is null.");
     if (result)
         *result = nullptr;
 
-    if (!gService->mInitialized)
+    if (!gService || !gService->mInitialized)
         return NS_ERROR_NOT_INITIALIZED;
 
     nsCacheRequest * request = nullptr;
 
     nsresult rv = gService->CreateRequest(session,
                                           key,
                                           accessRequested,
                                           blockingMode,
@@ -2349,22 +2346,22 @@ nsCacheService::DoomEntry_Internal(nsCac
     }
     return rv;
 }
 
 
 void
 nsCacheService::OnProfileShutdown()
 {
-    if (!gService)  return;
-    if (!gService->mInitialized) {
+    if (!gService || !gService->mInitialized) {
         // The cache service has been shut down, but someone is still holding
         // a reference to it. Ignore this call.
         return;
     }
+
     {
         nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHESERVICE_ONPROFILESHUTDOWN));
         gService->mClearingEntries = true;
         gService->DoomActiveEntries(nullptr);
     }
 
     gService->CloseAllStreams();