From d8c86787c4114f29f49971b86dcd353233f1a61a Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 14 Feb 2025 15:05:29 +0100 Subject: [PATCH] Ensure cluster cache is properly clean in case of exception in constructor. --- src/fileimpl.cpp | 5 ++++- test/archive.cpp | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/fileimpl.cpp b/src/fileimpl.cpp index f193f9f6..e3b8a316 100644 --- a/src/fileimpl.cpp +++ b/src/fileimpl.cpp @@ -188,7 +188,7 @@ class Grouping {} #endif - FileImpl::FileImpl(std::shared_ptr _zimFile) + FileImpl::FileImpl(std::shared_ptr _zimFile) try : zimFile(_zimFile), zimReader(makeFileReader(zimFile)), direntReader(new DirentReader(zimReader)), @@ -265,6 +265,9 @@ class Grouping m_byTitleDirentLookup.reset(new ByTitleDirentLookup(mp_titleDirentAccessor.get())); readMimeTypes(); + } catch (...) { + getClusterCache().drop_all([=](const std::tuple& key) {return std::get<0>(key) == this;}); + throw; } FileImpl::~FileImpl() { diff --git a/test/archive.cpp b/test/archive.cpp index 6dad4dc1..9a92f5a3 100644 --- a/test/archive.cpp +++ b/test/archive.cpp @@ -50,6 +50,9 @@ class ZimArchive: public testing::Test { zim::set_cluster_cache_max_size(CLUSTER_CACHE_SIZE); ASSERT_EQ(zim::get_cluster_cache_current_size(), 0); } + void TearDown() override { + ASSERT_EQ(zim::get_cluster_cache_current_size(), 0); + } }; using TestContextImpl = std::vector >; @@ -669,7 +672,8 @@ class CapturedStderr #define EXPECT_BROKEN_ZIMFILE(ZIMPATH, EXPECTED_STDERROR_TEXT) \ CapturedStderr stderror; \ EXPECT_FALSE(zim::validate(ZIMPATH, checksToRun)); \ - EXPECT_EQ(EXPECTED_STDERROR_TEXT, std::string(stderror)) << ZIMPATH; + EXPECT_EQ(EXPECTED_STDERROR_TEXT, std::string(stderror)) << ZIMPATH; \ + ASSERT_EQ(zim::get_cluster_cache_current_size(), 0); #define TEST_BROKEN_ZIM_NAME(ZIMNAME, EXPECTED) \ for(auto& testfile: getDataFilePath(ZIMNAME)) {EXPECT_BROKEN_ZIMFILE(testfile.path, EXPECTED)}