Bug 1286735 - Add after-shutdown nullchecks to the appcache code, r=michal a=ritu
authorHonza Bambas <honzab.moz@firemni.cz>
Wed, 27 Jul 2016 12:10:00 +0200
changeset 332648 6307dc2381b59770db93bf502a4855bf436f385a
parent 332647 e74314c1717e9406417014786f00d06f7c0e8b00
child 332649 d67e58e5ad8c4b7714af49488a32428f4660c76d
push id9888
push userkwierso@gmail.com
push dateWed, 10 Aug 2016 17:37:29 +0000
treeherdermozilla-aurora@f5a9d33557dd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal, ritu
bugs1286735
milestone50.0a2
Bug 1286735 - Add after-shutdown nullchecks to the appcache code, r=michal a=ritu
netwerk/cache/nsDiskCacheDeviceSQL.cpp
--- a/netwerk/cache/nsDiskCacheDeviceSQL.cpp
+++ b/netwerk/cache/nsDiskCacheDeviceSQL.cpp
@@ -922,40 +922,46 @@ nsOfflineCacheDevice::GetStrictFileOrigi
 
     // As default value use true (be more strict)
     return true;
 }
 
 uint32_t
 nsOfflineCacheDevice::CacheSize()
 {
+  NS_ENSURE_TRUE(Initialized(), 0);
+
   AutoResetStatement statement(mStatement_CacheSize);
 
   bool hasRows;
   nsresult rv = statement->ExecuteStep(&hasRows);
   NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasRows, 0);
   
   return (uint32_t) statement->AsInt32(0);
 }
 
 uint32_t
 nsOfflineCacheDevice::EntryCount()
 {
+  NS_ENSURE_TRUE(Initialized(), 0);
+
   AutoResetStatement statement(mStatement_EntryCount);
 
   bool hasRows;
   nsresult rv = statement->ExecuteStep(&hasRows);
   NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasRows, 0);
 
   return (uint32_t) statement->AsInt32(0);
 }
 
 nsresult
 nsOfflineCacheDevice::UpdateEntry(nsCacheEntry *entry)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   // Decompose the key into "ClientID" and "Key"
   nsAutoCString keyBuf;
   const char *cid, *key;
 
   if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
     return NS_ERROR_UNEXPECTED;
 
   // Store security info, if it is serializable
@@ -1029,16 +1035,18 @@ nsOfflineCacheDevice::UpdateEntry(nsCach
 
   NS_ASSERTION(!hasRows, "UPDATE should not result in output");
   return rv;
 }
 
 nsresult
 nsOfflineCacheDevice::UpdateEntrySize(nsCacheEntry *entry, uint32_t newSize)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   // Decompose the key into "ClientID" and "Key"
   nsAutoCString keyBuf;
   const char *cid, *key;
   if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
     return NS_ERROR_UNEXPECTED;
 
   AutoResetStatement statement(mStatement_UpdateEntrySize);
 
@@ -1059,16 +1067,18 @@ nsOfflineCacheDevice::UpdateEntrySize(ns
 
   NS_ASSERTION(!hasRows, "UPDATE should not result in output");
   return rv;
 }
 
 nsresult
 nsOfflineCacheDevice::DeleteEntry(nsCacheEntry *entry, bool deleteData)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   if (deleteData)
   {
     nsresult rv = DeleteData(entry);
     if (NS_FAILED(rv))
       return rv;
   }
 
   // Decompose the key into "ClientID" and "Key"
@@ -1328,16 +1338,18 @@ nsOfflineCacheDevice::BuildApplicationCa
   _result.Append(aOriginSuffix);
 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::InitActiveCaches()
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   MutexAutoLock lock(mLock);
 
   AutoResetStatement statement(mStatement_EnumerateGroups);
 
   bool hasRows;
   nsresult rv = statement->ExecuteStep(&hasRows);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1454,16 +1466,18 @@ const char *
 nsOfflineCacheDevice::GetDeviceID()
 {
   return OFFLINE_CACHE_DEVICE_ID;
 }
 
 nsCacheEntry *
 nsOfflineCacheDevice::FindEntry(nsCString *fullKey, bool *collision)
 {
+  NS_ENSURE_TRUE(Initialized(), nullptr);
+
   mozilla::Telemetry::AutoTimer<mozilla::Telemetry::CACHE_OFFLINE_SEARCH_2> timer;
   LOG(("nsOfflineCacheDevice::FindEntry [key=%s]\n", fullKey->get()));
 
   // SELECT * FROM moz_cache WHERE key = ?
 
   // Decompose the key into "ClientID" and "Key"
   nsAutoCString keyBuf;
   const char *cid, *key;
@@ -1563,16 +1577,18 @@ nsOfflineCacheDevice::DeactivateEntry(ns
   delete entry;
 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::BindEntry(nsCacheEntry *entry)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::BindEntry [key=%s]\n", entry->Key()->get()));
 
   NS_ENSURE_STATE(!entry->Data());
 
   // This method is called to inform us that we have a new entry.  The entry
   // may collide with an existing entry in our DB, but if that happens we can
   // assume that the entry is not being used.
 
@@ -1864,16 +1880,18 @@ nsOfflineCacheDevice::Visit(nsICacheVisi
 
   info->mRec = nullptr;
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::EvictEntries(const char *clientID)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::EvictEntries [cid=%s]\n",
        clientID ? clientID : ""));
 
   // called to evict all entries matching the given clientID.
 
   // need trigger to fire user defined function after a row is deleted
   // so we can delete the corresponding data file.
   EvictionObserver evictionObserver(mDB, mEvictionFunction);
@@ -1953,16 +1971,18 @@ nsOfflineCacheDevice::EvictEntries(const
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::MarkEntry(const nsCString &clientID,
                                 const nsACString &key,
                                 uint32_t typeBits)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::MarkEntry [cid=%s, key=%s, typeBits=%d]\n",
        clientID.get(), PromiseFlatCString(key).get(), typeBits));
 
   AutoResetStatement statement(mStatement_MarkEntry);
   nsresult rv = statement->BindInt32ByIndex(0, typeBits);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = statement->BindUTF8StringByIndex(1, clientID);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1975,16 +1995,18 @@ nsOfflineCacheDevice::MarkEntry(const ns
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::UnmarkEntry(const nsCString &clientID,
                                   const nsACString &key,
                                   uint32_t typeBits)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::UnmarkEntry [cid=%s, key=%s, typeBits=%d]\n",
        clientID.get(), PromiseFlatCString(key).get(), typeBits));
 
   AutoResetStatement statement(mStatement_UnmarkEntry);
   nsresult rv = statement->BindInt32ByIndex(0, typeBits);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = statement->BindUTF8StringByIndex(1, clientID);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -2012,16 +2034,18 @@ nsOfflineCacheDevice::UnmarkEntry(const 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::GetMatchingNamespace(const nsCString &clientID,
                                            const nsACString &key,
                                            nsIApplicationCacheNamespace **out)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::GetMatchingNamespace [cid=%s, key=%s]\n",
        clientID.get(), PromiseFlatCString(key).get()));
 
   nsresult rv;
 
   AutoResetStatement statement(mStatement_FindNamespaceEntry);
 
   rv = statement->BindUTF8StringByIndex(0, clientID);
@@ -2087,16 +2111,18 @@ nsOfflineCacheDevice::CacheOpportunistic
   return MarkEntry(clientID, key, nsIApplicationCache::ITEM_OPPORTUNISTIC);
 }
 
 nsresult
 nsOfflineCacheDevice::GetTypes(const nsCString &clientID,
                                const nsACString &key,
                                uint32_t *typeBits)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::GetTypes [cid=%s, key=%s]\n",
        clientID.get(), PromiseFlatCString(key).get()));
 
   AutoResetStatement statement(mStatement_GetTypes);
   nsresult rv = statement->BindUTF8StringByIndex(0, clientID);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = statement->BindUTF8StringByIndex(1, key);
   NS_ENSURE_SUCCESS(rv, rv);
@@ -2114,16 +2140,18 @@ nsOfflineCacheDevice::GetTypes(const nsC
 }
 
 nsresult
 nsOfflineCacheDevice::GatherEntries(const nsCString &clientID,
                                     uint32_t typeBits,
                                     uint32_t *count,
                                     char ***keys)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::GatherEntries [cid=%s, typeBits=%X]\n",
        clientID.get(), typeBits));
 
   AutoResetStatement statement(mStatement_GatherEntries);
   nsresult rv = statement->BindUTF8StringByIndex(0, clientID);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = statement->BindInt32ByIndex(1, typeBits);
@@ -2131,16 +2159,18 @@ nsOfflineCacheDevice::GatherEntries(cons
 
   return RunSimpleQuery(mStatement_GatherEntries, 0, count, keys);
 }
 
 nsresult
 nsOfflineCacheDevice::AddNamespace(const nsCString &clientID,
                                    nsIApplicationCacheNamespace *ns)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   nsCString namespaceSpec;
   nsresult rv = ns->GetNamespaceSpec(namespaceSpec);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCString data;
   rv = ns->GetData(data);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -2170,16 +2200,18 @@ nsOfflineCacheDevice::AddNamespace(const
 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::GetUsage(const nsACString &clientID,
                                uint32_t *usage)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::GetUsage [cid=%s]\n",
        PromiseFlatCString(clientID).get()));
 
   *usage = 0;
 
   AutoResetStatement statement(mStatement_ApplicationCacheSize);
 
   nsresult rv = statement->BindUTF8StringByIndex(0, clientID);
@@ -2196,25 +2228,29 @@ nsOfflineCacheDevice::GetUsage(const nsA
 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::GetGroups(uint32_t *count,
                                  char ***keys)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::GetGroups"));
 
   return RunSimpleQuery(mStatement_EnumerateGroups, 0, count, keys);
 }
 
 nsresult
 nsOfflineCacheDevice::GetGroupsTimeOrdered(uint32_t *count,
 					   char ***keys)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   LOG(("nsOfflineCacheDevice::GetGroupsTimeOrder"));
 
   return RunSimpleQuery(mStatement_EnumerateGroupsTimeOrder, 0, count, keys);
 }
 
 bool
 nsOfflineCacheDevice::IsLocked(const nsACString &key)
 {
@@ -2369,16 +2405,18 @@ nsOfflineCacheDevice::GetActiveCache(con
     return GetApplicationCache_Unlocked(*clientID, out);
 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::DeactivateGroup(const nsACString &group)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   nsCString *active = nullptr;
 
   AutoResetStatement statement(mStatement_DeactivateGroup);
   nsresult rv = statement->BindUTF8StringByIndex(0, group);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = statement->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
@@ -2393,16 +2431,18 @@ nsOfflineCacheDevice::DeactivateGroup(co
   }
 
   return NS_OK;
 }
 
 nsresult
 nsOfflineCacheDevice::Evict(nsILoadContextInfo *aInfo)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   NS_ENSURE_ARG(aInfo);
 
   nsresult rv;
 
   mozilla::OriginAttributes const *oa = aInfo->OriginAttributesPtr();
 
   if (oa->mAppId == NECKO_NO_APP_ID && oa->mInIsolatedMozBrowser == false) {
     nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID, &rv);
@@ -2497,16 +2537,18 @@ OriginMatch::OnFunctionCall(mozIStorageV
   return NS_OK;
 }
 
 } // anon
 
 nsresult
 nsOfflineCacheDevice::Evict(mozilla::OriginAttributesPattern const &aPattern)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   nsresult rv;
 
   nsCOMPtr<mozIStorageFunction> function1(new OriginMatch(aPattern));
   rv = mDB->CreateFunction(NS_LITERAL_CSTRING("ORIGIN_MATCH"), 1, function1);
   NS_ENSURE_SUCCESS(rv, rv);
 
   class AutoRemoveFunc {
   public:
@@ -2603,16 +2645,18 @@ nsOfflineCacheDevice::CanUseCache(nsIURI
 }
 
 
 nsresult
 nsOfflineCacheDevice::ChooseApplicationCache(const nsACString &key,
                                              nsILoadContextInfo *loadContextInfo,
                                              nsIApplicationCache **out)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   NS_ENSURE_ARG(loadContextInfo);
 
   nsresult rv;
 
   *out = nullptr;
 
   nsCOMPtr<nsIURI> keyURI;
   rv = NS_NewURI(getter_AddRefs(keyURI), key);
@@ -2695,16 +2739,18 @@ nsOfflineCacheDevice::CacheOpportunistic
 
   return CacheOpportunistically(clientID, key);
 }
 
 nsresult
 nsOfflineCacheDevice::ActivateCache(const nsCSubstring &group,
                                     const nsCSubstring &clientID)
 {
+  NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
+
   AutoResetStatement statement(mStatement_ActivateClient);
   nsresult rv = statement->BindUTF8StringByIndex(0, group);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = statement->BindUTF8StringByIndex(1, clientID);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = statement->BindInt32ByIndex(2, SecondsFromPRTime(PR_Now()));
   NS_ENSURE_SUCCESS(rv, rv);