Skip to content

Commit

Permalink
Fix potential race condition on m_direntLookup initialization.
Browse files Browse the repository at this point in the history
Reading and writing in the same time to a unique_ptr is not thread safe.
So the first read in `getDirent` (not lock protected) is a potential
race condition as we can also write to it (lock protected).

This is sad but we have to always get a lock to get the direntLookup.

Fix #945
  • Loading branch information
mgautierfr committed Feb 7, 2025
1 parent 88205b9 commit 433e1b4
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,12 @@ class Grouping
// in the call stack.
// 2. With `glibc` an exceptional execution of `std::call_once` doesn't
// unlock the mutex associated with the `std::once_flag` object.
std::lock_guard<std::mutex> lock(m_direntLookupCreationMutex);
if ( !m_direntLookup ) {
std::lock_guard<std::mutex> lock(m_direntLookupCreationMutex);
if ( !m_direntLookup ) {
if (m_direntLookupSize == 0) {
m_direntLookup = std::make_unique<DirentLookup>(mp_pathDirentAccessor.get());
} else {
m_direntLookup = std::make_unique<FastDirentLookup>(mp_pathDirentAccessor.get(), m_direntLookupSize);
}
if (m_direntLookupSize == 0) {
m_direntLookup = std::make_unique<DirentLookup>(mp_pathDirentAccessor.get());
} else {
m_direntLookup = std::make_unique<FastDirentLookup>(mp_pathDirentAccessor.get(), m_direntLookupSize);
}
}
return *m_direntLookup;
Expand Down

0 comments on commit 433e1b4

Please sign in to comment.