Skip to content

Commit

Permalink
fixup! Ensure cluster cache is properly clean in case of exception in…
Browse files Browse the repository at this point in the history
… constructor.

Calling non static member from constructor try block handler is UB.
Accessing this (witohut dereferencing it) is kind of gray area as
(See https://stackoverflow.com/questions/79463018/can-i-access-this-from-contructor-try-block-handler)

So the try/catch has been moved to a more classic try/catch block as
only a specific part of the contructor can create (and cache) clusters.
  • Loading branch information
mgautierfr committed Feb 24, 2025
1 parent 2a5a571 commit faa9871
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Grouping
{}
#endif

FileImpl::FileImpl(std::shared_ptr<FileCompound> _zimFile) try
FileImpl::FileImpl(std::shared_ptr<FileCompound> _zimFile)
: zimFile(_zimFile),
zimReader(makeFileReader(zimFile)),
direntReader(new DirentReader(zimReader)),
Expand Down Expand Up @@ -248,33 +248,42 @@ class Grouping
const_cast<entry_index_t&>(m_endUserEntry) = getCountArticles();
}

auto result = tmp_direntLookup.find('X', "listing/titleOrdered/v1");
if (result.first) {
mp_titleDirentAccessor = getTitleAccessorV1(result.second);
}

if (!mp_titleDirentAccessor) {
if (!header.hasTitleListingV0()) {
throw ZimFileFormatError("Zim file doesn't contain a title ordered index");
// Following code will may create cluster and
try {
auto result = tmp_direntLookup.find('X', "listing/titleOrdered/v1");
if (result.first) {
mp_titleDirentAccessor = getTitleAccessorV1(result.second);
}
offset_t titleOffset(header.getTitleIdxPos());
zsize_t titleSize(sizeof(entry_index_type)*header.getArticleCount());
mp_titleDirentAccessor = getTitleAccessor(titleOffset, titleSize, "Title index table");
const_cast<bool&>(m_hasFrontArticlesIndex) = false;
}
m_byTitleDirentLookup.reset(new ByTitleDirentLookup(mp_titleDirentAccessor.get()));

readMimeTypes();
} catch (...) {
getClusterCache().drop_all([=](const std::tuple<FileImpl*, cluster_index_type>& key) {return std::get<0>(key) == this;});
throw;
if (!mp_titleDirentAccessor) {
if (!header.hasTitleListingV0()) {
throw ZimFileFormatError("Zim file doesn't contain a title ordered index");
}
offset_t titleOffset(header.getTitleIdxPos());
zsize_t titleSize(sizeof(entry_index_type)*header.getArticleCount());
mp_titleDirentAccessor = getTitleAccessor(titleOffset, titleSize, "Title index table");
const_cast<bool&>(m_hasFrontArticlesIndex) = false;
}
m_byTitleDirentLookup.reset(new ByTitleDirentLookup(mp_titleDirentAccessor.get()));

readMimeTypes();
} catch (...) {
dropCachedCluster();
throw;
}
}

FileImpl::~FileImpl() {
// We have to clean the global cache for our clusters.
dropCachedCluster();
}

void FileImpl::dropCachedCluster() const {
getClusterCache().drop_all([=](const std::tuple<FileImpl*, cluster_index_type>& key) {return std::get<0>(key) == this;});
}


std::unique_ptr<IndirectDirentAccessor> FileImpl::getTitleAccessorV1(const entry_index_t idx)
{
auto dirent = mp_pathDirentAccessor->getDirent(idx);
Expand Down
2 changes: 2 additions & 0 deletions src/fileimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ namespace zim
explicit FileImpl(std::shared_ptr<FileCompound> zimFile);
FileImpl(std::shared_ptr<FileCompound> zimFile, offset_t offset, zsize_t size);

void dropCachedCluster() const;

std::unique_ptr<IndirectDirentAccessor> getTitleAccessorV1(const entry_index_t idx);
std::unique_ptr<IndirectDirentAccessor> getTitleAccessor(const offset_t offset, const zsize_t size, const std::string& name);

Expand Down

0 comments on commit faa9871

Please sign in to comment.