diff --git a/BAN/BAN/String.cpp b/BAN/BAN/String.cpp index 2429e1c4..609cf69d 100644 --- a/BAN/BAN/String.cpp +++ b/BAN/BAN/String.cpp @@ -214,7 +214,8 @@ namespace BAN void* new_data = BAN::allocator(new_cap); if (new_data == nullptr) return Error::FromString("String: Could not allocate memory"); - memcpy(new_data, m_data, m_size + 1); + if (m_data) + memcpy(new_data, m_data, m_size + 1); BAN::deallocator(m_data); m_data = (char*)new_data; m_capasity = new_cap; diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index 39a20842..89bfc1be 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -31,7 +31,7 @@ namespace BAN T& Front(); private: - [[nodiscard]] ErrorOr VerifyCapacity(size_type size); + [[nodiscard]] ErrorOr EnsureCapacity(size_type size); private: T* m_data = nullptr; @@ -50,7 +50,7 @@ namespace BAN template ErrorOr Queue::Push(const T& value) { - TRY(VerifyCapacity(m_size + 1)); + TRY(EnsureCapacity(m_size + 1)); m_data[m_size++] = value; return {}; } @@ -90,19 +90,17 @@ namespace BAN } template - ErrorOr Queue::VerifyCapacity(size_type size) + ErrorOr Queue::EnsureCapacity(size_type size) { if (m_capacity > size) return {}; - size_type new_cap = BAN::Math::max(size, m_capacity * 3 / 2); void* new_data = BAN::allocator(new_cap * sizeof(T)); if (new_data == nullptr) return Error::FromString("Queue: Could not allocate memory"); - - memcpy(new_data, m_data, m_size * sizeof(T)); + if (m_data) + memcpy(new_data, m_data, m_size * sizeof(T)); BAN::deallocator(m_data); - m_data = (T*)new_data; m_capacity = new_cap; diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 8b6ecef4..0665449c 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -211,7 +211,8 @@ namespace BAN void* new_data = BAN::allocator(new_cap * sizeof(T)); if (new_data == nullptr) return Error::FromString("Vector: Could not allocate memory"); - memcpy(new_data, m_data, m_size * sizeof(T)); + if (m_data) + memcpy(new_data, m_data, m_size * sizeof(T)); BAN::deallocator(m_data); m_data = (T*)new_data; m_capasity = new_cap;