Просмотр исходного кода

core: fix double-free when copying an empty object

Daniele Bartolini 5 лет назад
Родитель
Сommit
b679bd69ad
3 измененных файлов с 10 добавлено и 4 удалено
  1. 2 2
      src/core/containers/hash_map.inl
  2. 2 2
      src/core/containers/hash_set.inl
  3. 6 0
      src/core/unit_tests.cpp

+ 2 - 2
src/core/containers/hash_map.inl

@@ -344,9 +344,9 @@ HashMap<TKey, TValue, Hash, KeyEqual>::HashMap(const HashMap<TKey, TValue, Hash,
 	_size = other._size;
 	_mask = other._mask;
 
-	_allocator->deallocate(_buffer);
 	if (other._capacity > 0)
 	{
+		_allocator->deallocate(_buffer);
 		const u32 size = other._capacity * (sizeof(Index) + sizeof(Entry)) + alignof(Index) + alignof(Entry);
 		_buffer = (char*)_allocator->allocate(size);
 		_index = (Index*)memory::align_top(_buffer, alignof(Index));
@@ -381,9 +381,9 @@ HashMap<TKey, TValue, Hash, KeyEqual>& HashMap<TKey, TValue, Hash, KeyEqual>::op
 	_size = other._size;
 	_mask = other._mask;
 
-	_allocator->deallocate(_buffer);
 	if (other._capacity > 0)
 	{
+		_allocator->deallocate(_buffer);
 		const u32 size = other._capacity * (sizeof(Index) + sizeof(Entry)) + alignof(Index) + alignof(Entry);
 		_buffer = (char*)_allocator->allocate(size);
 		_index = (Index*)memory::align_top(_buffer, alignof(Index));

+ 2 - 2
src/core/containers/hash_set.inl

@@ -319,9 +319,9 @@ HashSet<TKey, Hash, KeyEqual>::HashSet(const HashSet& other)
 	_size = other._size;
 	_mask = other._mask;
 
-	_allocator->deallocate(_buffer);
 	if (other._capacity > 0)
 	{
+		_allocator->deallocate(_buffer);
 		const u32 size = other._capacity * (sizeof(Index) + sizeof(TKey)) + alignof(Index) + alignof(TKey);
 		_buffer = (char*)_allocator->allocate(size);
 		_index = (Index*)memory::align_top(_buffer, alignof(Index));
@@ -356,9 +356,9 @@ HashSet<TKey, Hash, KeyEqual>& HashSet<TKey, Hash, KeyEqual>::operator=(const Ha
 	_size = other._size;
 	_mask = other._mask;
 
-	_allocator->deallocate(_buffer);
 	if (other._capacity > 0)
 	{
+		_allocator->deallocate(_buffer);
 		const u32 size = other._capacity * (sizeof(Index) + sizeof(TKey)) + alignof(Index) + alignof(TKey);
 		_buffer = (char*)_allocator->allocate(size);
 		_index = (Index*)memory::align_top(_buffer, alignof(Index));

+ 6 - 0
src/core/unit_tests.cpp

@@ -150,6 +150,12 @@ static void test_hash_map()
 			hash_map::remove(m, i);
 		}
 	}
+	{
+		HashMap<s32, s32> ma(a);
+		HashMap<s32, s32> mb(a);
+		hash_map::set(ma, 0, 0);
+		ma = mb;
+	}
 	memory_globals::shutdown();
 }