From e9c41c33d230b54635a29223918e6aaeace3704c Mon Sep 17 00:00:00 2001 From: s1lent Date: Thu, 6 Jun 2019 00:34:40 +0700 Subject: [PATCH] Fix for range loop in CUtlVector (Sync from dreamstalker/rehlds#697) --- regamedll/public/utlmemory.h | 6 ++++-- regamedll/public/utlvector.h | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/regamedll/public/utlmemory.h b/regamedll/public/utlmemory.h index 4f2fc0fb..26706557 100644 --- a/regamedll/public/utlmemory.h +++ b/regamedll/public/utlmemory.h @@ -262,6 +262,7 @@ void CUtlMemory::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) @@ -274,6 +275,7 @@ void CUtlMemory::Grow(int num) { m_nAllocationCount += m_nAllocationCount; } + needToReallocate = true; } else { @@ -283,11 +285,11 @@ void CUtlMemory::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)); } diff --git a/regamedll/public/utlvector.h b/regamedll/public/utlvector.h index d341292a..cab40a2b 100644 --- a/regamedll/public/utlvector.h +++ b/regamedll/public/utlvector.h @@ -43,10 +43,10 @@ public: // 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 &operator=(const CUtlVector &other);