Bug 943021 - Safebrowsing service should use fallible arrays for its buffers. r=gcp
☠☠ backed out by c467462fae8e ☠ ☠
authorPeiyong Lin <pylaurent1314@gmail.com>
Mon, 06 Jan 2014 09:58:08 -0500
changeset 162234 77d3d487c68d98f4e984d78046312c6d265eb2b9
parent 162233 d73cb462dbed66086367aee8097913e236475fe5
child 162235 70c67626470c213903c74718cf510b1bdcfe6d5e
push id25941
push userkwierso@gmail.com
push dateTue, 07 Jan 2014 01:20:54 +0000
treeherdermozilla-central@ce917d3dd7c8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs943021
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 943021 - Safebrowsing service should use fallible arrays for its buffers. r=gcp
toolkit/components/url-classifier/Entries.h
toolkit/components/url-classifier/HashStore.cpp
--- a/toolkit/components/url-classifier/Entries.h
+++ b/toolkit/components/url-classifier/Entries.h
@@ -235,20 +235,20 @@ struct SubComplete {
     if (cmp != 0)
       return cmp;
     if (addChunk != aOther.addChunk)
       return addChunk - aOther.addChunk;
     return subChunk - aOther.subChunk;
   }
 };
 
-typedef nsTArray<AddPrefix>   AddPrefixArray;
-typedef nsTArray<AddComplete> AddCompleteArray;
-typedef nsTArray<SubPrefix>   SubPrefixArray;
-typedef nsTArray<SubComplete> SubCompleteArray;
+typedef FallibleTArray<AddPrefix>   AddPrefixArray;
+typedef FallibleTArray<AddComplete> AddCompleteArray;
+typedef FallibleTArray<SubPrefix>   SubPrefixArray;
+typedef FallibleTArray<SubComplete> SubCompleteArray;
 
 /**
  * Compares chunks by their add chunk, then their prefix.
  */
 template<class T>
 class EntryCompare {
 public:
   typedef T elem_type;
@@ -259,41 +259,41 @@ public:
   }
 };
 
 /**
  * Sort an array of store entries.  nsTArray::Sort uses Equal/LessThan
  * to sort, this does a single Compare so it's a bit quicker over the
  * large sorts we do.
  */
-template<class T>
+template<class T, class Alloc>
 void
-EntrySort(nsTArray<T>& aArray)
+EntrySort(nsTArray_Impl<T, Alloc>& aArray)
 {
   qsort(aArray.Elements(), aArray.Length(), sizeof(T),
         EntryCompare<T>::Compare);
 }
 
-template<class T>
+template<class T, class Alloc>
 nsresult
-ReadTArray(nsIInputStream* aStream, nsTArray<T>* aArray, uint32_t aNumElements)
+ReadTArray(nsIInputStream* aStream, nsTArray_Impl<T, Alloc>* aArray, uint32_t aNumElements)
 {
   if (!aArray->SetLength(aNumElements))
     return NS_ERROR_OUT_OF_MEMORY;
 
   void *buffer = aArray->Elements();
   nsresult rv = NS_ReadInputStreamToBuffer(aStream, &buffer,
                                            (aNumElements * sizeof(T)));
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
-template<class T>
+template<class T, class Alloc>
 nsresult
-WriteTArray(nsIOutputStream* aStream, nsTArray<T>& aArray)
+WriteTArray(nsIOutputStream* aStream, nsTArray_Impl<T, Alloc>& aArray)
 {
   uint32_t written;
   return aStream->Write(reinterpret_cast<char*>(aArray.Elements()),
                         aArray.Length() * sizeof(T),
                         &written);
 }
 
 } // namespace safebrowsing
--- a/toolkit/components/url-classifier/HashStore.cpp
+++ b/toolkit/components/url-classifier/HashStore.cpp
@@ -428,19 +428,19 @@ HashStore::BeginUpdate()
   mInUpdate = true;
 
   return NS_OK;
 }
 
 template<class T>
 static nsresult
 Merge(ChunkSet* aStoreChunks,
-      nsTArray<T>* aStorePrefixes,
+      FallibleTArray<T>* aStorePrefixes,
       ChunkSet& aUpdateChunks,
-      nsTArray<T>& aUpdatePrefixes,
+      FallibleTArray<T>& aUpdatePrefixes,
       bool aAllowMerging = false)
 {
   EntrySort(aUpdatePrefixes);
 
   T* updateIter = aUpdatePrefixes.Elements();
   T* updateEnd = aUpdatePrefixes.Elements() + aUpdatePrefixes.Length();
 
   T* storeIter = aStorePrefixes->Elements();
@@ -533,17 +533,17 @@ HashStore::ClearCompletes()
   mAddCompletes.Clear();
   mSubCompletes.Clear();
 
   UpdateHeader();
 }
 
 template<class T>
 static void
-ExpireEntries(nsTArray<T>* aEntries, ChunkSet& aExpirations)
+ExpireEntries(FallibleTArray<T>* aEntries, ChunkSet& aExpirations)
 {
   T* addIter = aEntries->Elements();
   T* end = aEntries->Elements() + aEntries->Length();
 
   for (T *iter = addIter; iter != end; iter++) {
     if (!aExpirations.Has(iter->Chunk())) {
       *addIter = *iter;
       addIter++;
@@ -570,18 +570,20 @@ HashStore::Expire()
   return NS_OK;
 }
 
 template<class T>
 nsresult DeflateWriteTArray(nsIOutputStream* aStream, nsTArray<T>& aIn)
 {
   uLongf insize = aIn.Length() * sizeof(T);
   uLongf outsize = compressBound(insize);
-  nsTArray<char> outBuff;
-  outBuff.SetLength(outsize);
+  FallibleTArray<char> outBuff;
+  if (!outBuff.SetLength(outsize)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
 
   int zerr = compress(reinterpret_cast<Bytef*>(outBuff.Elements()),
                       &outsize,
                       reinterpret_cast<const Bytef*>(aIn.Elements()),
                       insize);
   if (zerr != Z_OK) {
     return NS_ERROR_FAILURE;
   }
@@ -711,17 +713,19 @@ nsresult
 HashStore::ReadAddPrefixes()
 {
   FallibleTArray<uint32_t> chunks;
   uint32_t count = mHeader.numAddPrefixes;
 
   nsresult rv = ByteSliceRead(mInputStream, &chunks, count);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  mAddPrefixes.SetCapacity(count);
+  if (!mAddPrefixes.SetCapacity(count)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
   for (uint32_t i = 0; i < count; i++) {
     AddPrefix *add = mAddPrefixes.AppendElement();
     add->prefix.FromUint32(0);
     add->addChunk = chunks[i];
   }
 
   return NS_OK;
 }
@@ -738,17 +742,19 @@ HashStore::ReadSubPrefixes()
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = ByteSliceRead(mInputStream, &subchunks, count);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = ByteSliceRead(mInputStream, &prefixes, count);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  mSubPrefixes.SetCapacity(count);
+  if (!mSubPrefixes.SetCapacity(count)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
   for (uint32_t i = 0; i < count; i++) {
     SubPrefix *sub = mSubPrefixes.AppendElement();
     sub->addChunk = addchunks[i];
     sub->prefix.FromUint32(prefixes[i]);
     sub->subChunk = subchunks[i];
   }
 
   return NS_OK;
@@ -847,17 +853,17 @@ HashStore::WriteFile()
   rv = safeOut->Finish();
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 template <class T>
 static void
-Erase(nsTArray<T>* array, T* iterStart, T* iterEnd)
+Erase(FallibleTArray<T>* array, T* iterStart, T* iterEnd)
 {
   uint32_t start = iterStart - array->Elements();
   uint32_t count = iterEnd - iterStart;
 
   if (count > 0) {
     array->RemoveElementsAt(start, count);
   }
 }
@@ -867,17 +873,17 @@ Erase(nsTArray<T>* array, T* iterStart, 
 // copies, the inputs are processing in parallel, so |subs| and |adds|
 // should be compatibly ordered (either by SBAddPrefixLess or
 // SBAddPrefixHashLess).
 //
 // |predAS| provides add < sub, |predSA| provides sub < add, for the
 // tightest compare appropriate (see calls in SBProcessSubs).
 template<class TSub, class TAdd>
 static void
-KnockoutSubs(nsTArray<TSub>* aSubs, nsTArray<TAdd>* aAdds)
+KnockoutSubs(FallibleTArray<TSub>* aSubs, FallibleTArray<TAdd>* aAdds)
 {
   // Keep a pair of output iterators for writing kept items.  Due to
   // deletions, these may lag the main iterators.  Using erase() on
   // individual items would result in O(N^2) copies.  Using a list
   // would work around that, at double or triple the memory cost.
   TAdd* addOut = aAdds->Elements();
   TAdd* addIter = aAdds->Elements();
 
@@ -910,17 +916,17 @@ KnockoutSubs(nsTArray<TSub>* aSubs, nsTA
   Erase(aAdds, addOut, addIter);
   Erase(aSubs, subOut, subIter);
 }
 
 // Remove items in |removes| from |fullHashes|.  |fullHashes| and
 // |removes| should be ordered by SBAddPrefix component.
 template <class T>
 static void
-RemoveMatchingPrefixes(const SubPrefixArray& aSubs, nsTArray<T>* aFullHashes)
+RemoveMatchingPrefixes(const SubPrefixArray& aSubs, FallibleTArray<T>* aFullHashes)
 {
   // Where to store kept items.
   T* out = aFullHashes->Elements();
   T* hashIter = out;
   T* hashEnd = aFullHashes->Elements() + aFullHashes->Length();
 
   SubPrefix const * removeIter = aSubs.Elements();
   SubPrefix const * removeEnd = aSubs.Elements() + aSubs.Length();