8216426: Usage of array placement new may lead to memory corruption

Reviewed-by: rehn, kbarrett, rkennke, eosterlund
This commit is contained in:
Martin Doerr 2019-01-15 10:23:23 +01:00
parent 744d6085b4
commit b2f82b3bd3
2 changed files with 7 additions and 3 deletions

View File

@ -73,7 +73,7 @@ class ConcurrentHashTable : public CHeapObj<F> {
void print_value_on(outputStream* st) const {};
};
// Only constructed with placement new[] from an array allocated with MEMFLAGS
// Only constructed with placement new from an array allocated with MEMFLAGS
// of InternalTable.
class Bucket {
private:

View File

@ -193,8 +193,12 @@ inline ConcurrentHashTable<VALUE, CONFIG, F>::
{
assert(_log2_size >= SIZE_SMALL_LOG2 && _log2_size <= SIZE_BIG_LOG2,
"Bad size");
void* memory = NEW_C_HEAP_ARRAY(Bucket, _size, F);
_buckets = new (memory) Bucket[_size];
_buckets = NEW_C_HEAP_ARRAY(Bucket, _size, F);
// Use placement new for each element instead of new[] which could use more
// memory than allocated.
for (size_t i = 0; i < _size; ++i) {
new (_buckets + i) Bucket();
}
}
template <typename VALUE, typename CONFIG, MEMFLAGS F>