Skip to content

Commit 7a217b7

Browse files
committed
Add casts to avoid integer overflow.
I'm quite sure that this has never mattered in this library, and if we ever actually allocate more than 4GB in a single allocation in these containers, we are probably in bad shape. Still, this will stop noise from static analysis tools reporting potential vulnerabilities. This addresses issue #363
1 parent 10f2446 commit 7a217b7

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/tier1/utlmemory.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ m_nAllocationCount( nInitAllocationCount ), m_nGrowSize( nGrowSize ), m_unSizeOf
1818
if (m_nAllocationCount)
1919
{
2020
UTLMEMORY_TRACK_ALLOC();
21-
m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements );
21+
m_pMemory = PvAlloc( (size_t)m_nAllocationCount * m_unSizeOfElements );
2222
}
2323
}
2424

@@ -101,7 +101,7 @@ void CUtlMemoryBase::ConvertToGrowableMemory( int nGrowSize )
101101
UTLMEMORY_TRACK_ALLOC();
102102
MEM_ALLOC_CREDIT_CLASS();
103103

104-
int nNumBytes = m_nAllocationCount * m_unSizeOfElements;
104+
size_t nNumBytes = (size_t)m_nAllocationCount * m_unSizeOfElements;
105105
void *pMemory = PvAlloc( nNumBytes );
106106
memcpy( pMemory, m_pMemory, nNumBytes );
107107
m_pMemory = pMemory;
@@ -263,11 +263,11 @@ void CUtlMemoryBase::Grow( int num )
263263
UTLMEMORY_TRACK_ALLOC();
264264
if (m_pMemory)
265265
{
266-
m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements );
266+
m_pMemory = PvRealloc( m_pMemory, (size_t)m_nAllocationCount * m_unSizeOfElements );
267267
}
268268
else
269269
{
270-
m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements );
270+
m_pMemory = PvAlloc( (size_t)m_nAllocationCount * m_unSizeOfElements );
271271
}
272272
}
273273

@@ -295,11 +295,11 @@ void CUtlMemoryBase::EnsureCapacity( int num )
295295

296296
if (m_pMemory)
297297
{
298-
m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements );
298+
m_pMemory = PvRealloc( m_pMemory, (size_t)m_nAllocationCount * m_unSizeOfElements );
299299
}
300300
else
301301
{
302-
m_pMemory = PvAlloc( m_nAllocationCount * m_unSizeOfElements );
302+
m_pMemory = PvAlloc( (size_t)m_nAllocationCount * m_unSizeOfElements );
303303
}
304304
}
305305

@@ -369,7 +369,7 @@ void CUtlMemoryBase::Purge( int numElements, bool bRealloc )
369369

370370
// Allocation count > 0, shrink it down.
371371
MEM_ALLOC_CREDIT_CLASS();
372-
m_pMemory = PvRealloc( m_pMemory, m_nAllocationCount * m_unSizeOfElements );
372+
m_pMemory = PvRealloc( m_pMemory, (size_t)m_nAllocationCount * m_unSizeOfElements );
373373
}
374374
else
375375
{

0 commit comments

Comments
 (0)