Skip to content

Commit 433e1b4

Browse files
committed
Fix potential race condition on m_direntLookup initialization.
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
1 parent 88205b9 commit 433e1b4

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/fileimpl.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,12 @@ class Grouping
294294
// in the call stack.
295295
// 2. With `glibc` an exceptional execution of `std::call_once` doesn't
296296
// unlock the mutex associated with the `std::once_flag` object.
297+
std::lock_guard<std::mutex> lock(m_direntLookupCreationMutex);
297298
if ( !m_direntLookup ) {
298-
std::lock_guard<std::mutex> lock(m_direntLookupCreationMutex);
299-
if ( !m_direntLookup ) {
300-
if (m_direntLookupSize == 0) {
301-
m_direntLookup = std::make_unique<DirentLookup>(mp_pathDirentAccessor.get());
302-
} else {
303-
m_direntLookup = std::make_unique<FastDirentLookup>(mp_pathDirentAccessor.get(), m_direntLookupSize);
304-
}
299+
if (m_direntLookupSize == 0) {
300+
m_direntLookup = std::make_unique<DirentLookup>(mp_pathDirentAccessor.get());
301+
} else {
302+
m_direntLookup = std::make_unique<FastDirentLookup>(mp_pathDirentAccessor.get(), m_direntLookupSize);
305303
}
306304
}
307305
return *m_direntLookup;

0 commit comments

Comments
 (0)