Skip to content

Commit

Permalink
Fix for range loop in CUtlVector (Sync from dreamstalker/rehlds#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed Jun 5, 2019
1 parent 309d5c5 commit e9c41c3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions regamedll/public/utlmemory.h
Expand Up @@ -262,6 +262,7 @@ void CUtlMemory<T, I>::Grow(int num)
// Make sure we have at least numallocated + num allocations.
// Use the grow rules specified for this memory (in m_nGrowSize)
int nAllocationRequested = m_nAllocationCount + num;
bool needToReallocate = false;
while (m_nAllocationCount < nAllocationRequested)
{
if (m_nAllocationCount != 0)
Expand All @@ -274,6 +275,7 @@ void CUtlMemory<T, I>::Grow(int num)
{
m_nAllocationCount += m_nAllocationCount;
}
needToReallocate = true;
}
else
{
Expand All @@ -283,11 +285,11 @@ void CUtlMemory<T, I>::Grow(int num)
}
}

if (m_pMemory)
if (m_pMemory && needToReallocate)
{
m_pMemory = (T *)realloc(m_pMemory, m_nAllocationCount * sizeof(T));
}
else
else if (!m_pMemory)
{
m_pMemory = (T *)malloc(m_nAllocationCount * sizeof(T));
}
Expand Down
4 changes: 2 additions & 2 deletions regamedll/public/utlvector.h
Expand Up @@ -43,10 +43,10 @@ class CUtlVector

// features C++11 ranged based for
T *begin() { return &m_Memory[0]; }
T *end() { return &m_Memory[m_Size - 1]; }
T *end() { return &m_Memory[m_Size]; }

T const *begin() const { return &m_Memory[0]; }
T const *end() const { return &m_Memory[m_Size - 1]; }
T const *end() const { return &m_Memory[m_Size]; }

// Copy the array.
CUtlVector<T> &operator=(const CUtlVector<T> &other);
Expand Down

0 comments on commit e9c41c3

Please sign in to comment.