Browse Source

Bugfix: TempAllocator could forward zero sized allocations

Also added asserts to the allocation hooks in the sample app to catch this situation earlier in the future

Fixes #1102
Jorrit Rouwe 1 year ago
parent
commit
3ae9520be6
2 changed files with 6 additions and 4 deletions
  1. 3 2
      Jolt/Core/TempAllocator.h
  2. 3 2
      TestFramework/Utils/CustomMemoryHook.cpp

+ 3 - 2
Jolt/Core/TempAllocator.h

@@ -108,13 +108,14 @@ public:
 	// See: TempAllocator
 	// See: TempAllocator
 	virtual void *					Allocate(uint inSize) override
 	virtual void *					Allocate(uint inSize) override
 	{
 	{
-		return AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT);
+		return inSize > 0? AlignedAllocate(inSize, JPH_RVECTOR_ALIGNMENT) : nullptr;
 	}
 	}
 
 
 	// See: TempAllocator
 	// See: TempAllocator
 	virtual void					Free(void *inAddress, [[maybe_unused]] uint inSize) override
 	virtual void					Free(void *inAddress, [[maybe_unused]] uint inSize) override
 	{
 	{
-		AlignedFree(inAddress);
+		if (inAddress != nullptr)
+			AlignedFree(inAddress);
 	}
 	}
 };
 };
 
 

+ 3 - 2
TestFramework/Utils/CustomMemoryHook.cpp

@@ -58,12 +58,14 @@ static void *UntagAllocation(void *inPointer, size_t inAlignment, char inMode)
 
 
 static void *AllocateHook(size_t inSize)
 static void *AllocateHook(size_t inSize)
 {
 {
+	JPH_ASSERT(inSize > 0);
 	InCustomAllocator ica;
 	InCustomAllocator ica;
 	return TagAllocation(malloc(inSize + 16), 16, 'U');
 	return TagAllocation(malloc(inSize + 16), 16, 'U');
 }
 }
 
 
 static void *ReallocateHook(void *inBlock, size_t inSize)
 static void *ReallocateHook(void *inBlock, size_t inSize)
 {
 {
+	JPH_ASSERT(inSize > 0);
 	InCustomAllocator ica;
 	InCustomAllocator ica;
 	return TagAllocation(realloc(UntagAllocation(inBlock, 16, 'U'), inSize + 16), 16, 'U');
 	return TagAllocation(realloc(UntagAllocation(inBlock, 16, 'U'), inSize + 16), 16, 'U');
 }
 }
@@ -76,8 +78,7 @@ static void FreeHook(void *inBlock)
 
 
 static void *AlignedAllocateHook(size_t inSize, size_t inAlignment)
 static void *AlignedAllocateHook(size_t inSize, size_t inAlignment)
 {
 {
-	JPH_ASSERT(inAlignment <= 64);
-
+	JPH_ASSERT(inSize > 0 && inAlignment > 0 && inAlignment <= 64);
 	InCustomAllocator ica;
 	InCustomAllocator ica;
 	return TagAllocation(_aligned_malloc(inSize + 64, inAlignment), 64, 'A');
 	return TagAllocation(_aligned_malloc(inSize + 64, inAlignment), 64, 'A');
 }
 }