From 41203df4725f5f648398fe7815a764ca155c4f24 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 10 Mar 2023 21:07:08 +0100 Subject: [PATCH] libluna/Vector: Use bytes instead of count in resize() This fixes a bug that was causing the heap linked list to become quite corrupted. --- libluna/include/luna/Vector.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libluna/include/luna/Vector.h b/libluna/include/luna/Vector.h index c74f632e..c5f61552 100644 --- a/libluna/include/luna/Vector.h +++ b/libluna/include/luna/Vector.h @@ -150,6 +150,11 @@ template class Vector return m_capacity; } + usize byte_capacity() const + { + return m_capacity * sizeof(T); + } + usize size() const { return m_size; @@ -162,10 +167,14 @@ template class Vector Result resize(usize new_capacity) { - void* ptr = TRY(realloc_impl(m_data, new_capacity)); - if (new_capacity < m_capacity) memcpy(ptr, m_data, new_capacity); + const usize new_byte_capacity = new_capacity * sizeof(T); + + void* const ptr = TRY(realloc_impl(m_data, new_byte_capacity)); + + if (new_capacity < m_capacity) memcpy(ptr, m_data, new_byte_capacity); else - memcpy(ptr, m_data, m_capacity); + memcpy(ptr, m_data, byte_capacity()); + m_capacity = new_capacity; m_data = (T*)ptr; return {};