From 0e32f3b957d9a96bfd66cf4d3fbf063a57d8d1f0 Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Sat, 21 Dec 2024 03:03:37 +0100 Subject: [PATCH] Make `LocalVector` -> `Vector` automatic conversion safe for non-trivial types. --- core/templates/local_vector.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index b59f86879760..d20fe49eba6f 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -295,10 +295,16 @@ class LocalVector { operator Vector() const { Vector ret; - ret.resize(size()); + ret.resize(count); T *w = ret.ptrw(); if (w) { - memcpy(w, data, sizeof(T) * count); + if constexpr (std::is_trivially_copyable_v) { + memcpy(w, data, sizeof(T) * count); + } else { + for (U i = 0; i < count; i++) { + w[i] = data[i]; + } + } } return ret; }