Bug 1452288 - Use calloc for allocating PLDHashTable entries. r=froydnj
--- 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.