Bug 1452288 - Use calloc for allocating PLDHashTable entries. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Fri, 06 Apr 2018 16:51:58 -0700
changeset 779604 4cd93e695965adeb76237e41d03c4a6d52ab27d1
parent 779603 f45d6ae3fdb194a07ef5eba9f9a760f28f564b64
child 779605 38dc14aea22e74d861d898239b4193c1d90d615b
push id105824
push usernnethercote@mozilla.com
push dateTue, 10 Apr 2018 09:55:33 +0000
reviewersfroydnj
bugs1452288
milestone61.0a1
Bug 1452288 - Use calloc for allocating PLDHashTable entries. r=froydnj
xpcom/ds/PLDHashTable.cpp
--- a/xpcom/ds/PLDHashTable.cpp
+++ b/xpcom/ds/PLDHashTable.cpp
@@ -471,27 +471,26 @@ PLDHashTable::ChangeTable(int32_t aDelta
     return false;
   }
 
   uint32_t nbytes;
   if (!SizeOfEntryStore(newCapacity, mEntrySize, &nbytes)) {
     return false;   // overflowed
   }
 
-  char* newEntryStore = (char*)malloc(nbytes);
+  char* newEntryStore = (char*)calloc(1, nbytes);
   if (!newEntryStore) {
     return false;
   }
 
   // We can't fail from here on, so update table parameters.
   mHashShift = kHashBits - newLog2;
   mRemovedCount = 0;
 
   // Assign the new entry store to table.
-  memset(newEntryStore, 0, nbytes);
   char* oldEntryStore;
   char* oldEntryAddr;
   oldEntryAddr = oldEntryStore = mEntryStore.Get();
   mEntryStore.Set(newEntryStore, &mGeneration);
   PLDHashMoveEntry moveEntry = mOps->moveEntry;
 
   // Copy only live entries, leaving removed ones behind.
   uint32_t oldCapacity = 1u << oldLog2;
@@ -550,21 +549,20 @@ PLDHashTable::Add(const void* aKey, cons
 #endif
 
   // Allocate the entry storage if it hasn't already been allocated.
   if (!mEntryStore.Get()) {
     uint32_t nbytes;
     // We already checked this in the constructor, so it must still be true.
     MOZ_RELEASE_ASSERT(SizeOfEntryStore(CapacityFromHashShift(), mEntrySize,
                                         &nbytes));
-    mEntryStore.Set((char*)malloc(nbytes), &mGeneration);
+    mEntryStore.Set((char*)calloc(1, nbytes), &mGeneration);
     if (!mEntryStore.Get()) {
       return nullptr;
     }
-    memset(mEntryStore.Get(), 0, nbytes);
   }
 
   // If alpha is >= .75, grow or compress the table. If aKey is already in the
   // table, we may grow once more than necessary, but only if we are on the
   // edge of being overloaded.
   uint32_t capacity = Capacity();
   if (mEntryCount + mRemovedCount >= MaxLoad(capacity)) {
     // Compress if a quarter or more of all entries are removed.