From 2c0e40a21f95f62ba9bcfe59eec3ac36baebe5cb Mon Sep 17 00:00:00 2001 From: Benny Halevy Date: Wed, 27 Mar 2024 14:36:57 +0200 Subject: [PATCH] utils: chunked_vector: push_back: call emplace_back When pushing an element with a value referencing an exisiting element in the vector, we currently risking use-after-free when that element gets moved to a reallocated chunk, if capacity needs to be reserved, by that, invaliding the refernce to the existing element before it is used. This patch prepares for fixing that in the emplace path by converging to a single code path. Signed-off-by: Benny Halevy --- utils/chunked_vector.hh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/utils/chunked_vector.hh b/utils/chunked_vector.hh index 2018999356..896e56eb97 100644 --- a/utils/chunked_vector.hh +++ b/utils/chunked_vector.hh @@ -139,14 +139,10 @@ public: } void push_back(const T& x) { - reserve_for_push_back(); - new (addr(_size)) T(x); - ++_size; + emplace_back(x); } void push_back(T&& x) { - reserve_for_push_back(); - new (addr(_size)) T(std::move(x)); - ++_size; + emplace_back(std::move(x)); } template T& emplace_back(Args&&... args) {