From 792e5155bacf351b4729fef59131903c8ccaae75 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 15 Nov 2023 12:00:48 -0800 Subject: [PATCH] [Glacier] When resizing vector use the proper T constructor. Previously when we static_casted from uint8_t[] to T[] we ended up not properly initializing the objects in the array. This caused issues where garbage memory provided by new was treated as a legitimate object. Potentially in the future it would make sense to back vectors with a simple byte array and do memcpys to move objects in and out as needed. --- lib/glacier/container/vector.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/glacier/container/vector.h b/lib/glacier/container/vector.h index e66864a..7d81cf6 100644 --- a/lib/glacier/container/vector.h +++ b/lib/glacier/container/vector.h @@ -69,11 +69,13 @@ class Vector { template void Vector::Resize(uint64_t capacity) { - T* new_data = reinterpret_cast(new uint8_t[capacity * sizeof(T)]); - for (uint64_t i = 0; i < size_; i++) { - new_data[i] = glcr::Move(data_[i]); + T* new_data = new T[capacity]; + if (data_) { + for (uint64_t i = 0; i < size_; i++) { + new_data[i] = glcr::Move(data_[i]); + } + delete[] data_; } - delete[] data_; data_ = new_data; capacity_ = capacity; }