From 31b77344078a6a65dec6b2ceaba6923f9f4d7138 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Fri, 19 Mar 2021 00:26:02 +0300 Subject: [PATCH] LRUCache fix exception unsafe element insertion --- src/Common/LRUCache.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Common/LRUCache.h b/src/Common/LRUCache.h index d75c8caf1fc..012ab7fe6c4 100644 --- a/src/Common/LRUCache.h +++ b/src/Common/LRUCache.h @@ -271,16 +271,23 @@ private: void setImpl(const Key & key, const MappedPtr & mapped, [[maybe_unused]] std::lock_guard & cache_lock) { - auto res = cells.emplace(std::piecewise_construct, + auto [it, inserted] = cells.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple()); - Cell & cell = res.first->second; - bool inserted = res.second; + Cell & cell = it->second; if (inserted) { - cell.queue_iterator = queue.insert(queue.end(), key); + try + { + cell.queue_iterator = queue.insert(queue.end(), key); + } + catch (...) + { + cells.erase(it); + throw; + } } else {