Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CCPMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const int CCP_MEMORY_GUARD_SIZE_BACK = 64;
const int CCP_MEMORY_GUARD_SIZE = CCP_MEMORY_GUARD_SIZE_FRONT + CCP_MEMORY_GUARD_SIZE_BACK + sizeof( size_t );
const uint8_t CCP_MEMORY_GUARD_VALUE_FRONT = 0xee;
const uint8_t CCP_MEMORY_GUARD_VALUE_BACK = 0xef;
const size_t CCP_MEMORY_FREED_PATTERN = static_cast<size_t>( 0xddddddddddddddddULL );

// Should memory tracking in Telemetry be enabled? Defaults to yes. This can skew results of
// timing analysis of functions that do a lot of allocations, such as the yaml parser.
Expand Down Expand Up @@ -494,7 +495,7 @@ void CCPFreeWithGuard( void* p )
// Get the size
size_t orgSize = *(size_t*)p0;

if( orgSize == 0xdddddddd )
if( orgSize == CCP_MEMORY_FREED_PATTERN )
{
CCP_LOGERR( "Freeing a block that was already freed at 0x%p", p );
CCP_DEBUG_BREAK();
Expand Down Expand Up @@ -596,7 +597,7 @@ void CCPAlignedFree( void* p )

if( s_guardAllocations )
{
if( (uintptr_t)allocatedPtr == 0xdddddddd )
if( (uintptr_t)allocatedPtr == CCP_MEMORY_FREED_PATTERN )
{
CCP_LOGERR( "Freeing an aligned block that was already freed at 0x%p", p );
CCP_DEBUG_BREAK();
Expand Down
13 changes: 13 additions & 0 deletions tests/CcpMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ TEST( CcpMemory, CanDetectInvalidMemoryAccessBeforeMemoryBlock )
}
}

TEST( CcpMemory, CanDetectDoubleFreeWithGuard )
{
const size_t size = 123;
void* memory = CCPMallocWithGuard( size );
EXPECT_NE( nullptr, memory );
CCPFreeWithGuard( memory );

LogHelper logHelper;
SilencedFreeWithGuard( memory );

EXPECT_TRUE( logHelper.HasMessage( CCP::LOGTYPE_ERR, "already freed" ) );
}

TEST( CcpMemory, CanDuplicateString )
{
const char* string = "just a string";
Expand Down