1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 21:35:45 +02:00

bees: fix further instances of copy-after-unlock bug

Before:

        unique_lock<mutex> lock(some_mutex);
        // run lock.~unique_lock() because return
        // return reference to unprotected heap
        return foo[bar];

After:

        unique_lock<mutex> lock(some_mutex);
        // make copy of object on heap protected by mutex lock
        auto tmp_copy = foo[bar];
        // run lock.~unique_lock() because return
        // pass locally allocated object to copy constructor
        return tmp_copy;

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell 2017-01-22 21:50:21 -05:00
parent 6099bf0b01
commit db8ea92133
3 changed files with 16 additions and 8 deletions

View File

@ -969,7 +969,8 @@ BeesContext::tmpfile()
if (!m_tmpfiles[this_thread::get_id()]) { if (!m_tmpfiles[this_thread::get_id()]) {
m_tmpfiles[this_thread::get_id()] = make_shared<BeesTempFile>(shared_from_this()); m_tmpfiles[this_thread::get_id()] = make_shared<BeesTempFile>(shared_from_this());
} }
return m_tmpfiles[this_thread::get_id()]; auto rv = m_tmpfiles[this_thread::get_id()];
return rv;
} }
shared_ptr<BeesFdCache> shared_ptr<BeesFdCache>
@ -980,7 +981,8 @@ BeesContext::fd_cache()
if (!m_fd_cache) { if (!m_fd_cache) {
m_fd_cache = make_shared<BeesFdCache>(); m_fd_cache = make_shared<BeesFdCache>();
} }
return m_fd_cache; auto rv = m_fd_cache;
return rv;
} }
shared_ptr<BeesRoots> shared_ptr<BeesRoots>
@ -991,7 +993,8 @@ BeesContext::roots()
if (!m_roots) { if (!m_roots) {
m_roots = make_shared<BeesRoots>(shared_from_this()); m_roots = make_shared<BeesRoots>(shared_from_this());
} }
return m_roots; auto rv = m_roots;
return rv;
} }
shared_ptr<BeesHashTable> shared_ptr<BeesHashTable>
@ -1002,7 +1005,8 @@ BeesContext::hash_table()
if (!m_hash_table) { if (!m_hash_table) {
m_hash_table = make_shared<BeesHashTable>(shared_from_this(), "beeshash.dat"); m_hash_table = make_shared<BeesHashTable>(shared_from_this(), "beeshash.dat");
} }
return m_hash_table; auto rv = m_hash_table;
return rv;
} }
void void

View File

@ -822,7 +822,8 @@ BeesCrawl::peek_front()
if (m_extents.empty()) { if (m_extents.empty()) {
return BeesFileRange(); return BeesFileRange();
} }
return *m_extents.begin(); auto rv = *m_extents.begin();
return rv;
} }
BeesFileRange BeesFileRange
@ -848,7 +849,8 @@ BeesCrawlState
BeesCrawl::get_state() BeesCrawl::get_state()
{ {
unique_lock<mutex> lock(m_state_mutex); unique_lock<mutex> lock(m_state_mutex);
return m_state; auto rv = m_state;
return rv;
} }
void void

View File

@ -286,7 +286,8 @@ Fd
BeesFileRange::fd() const BeesFileRange::fd() const
{ {
unique_lock<mutex> lock(s_mutex); unique_lock<mutex> lock(s_mutex);
return m_fd; auto rv = m_fd;
return rv;
} }
Fd Fd
@ -310,7 +311,8 @@ BeesFileRange::fd(const shared_ptr<BeesContext> &ctx) const
} }
} }
// We either had a fid and opened it, or we didn't and we're just stuck with our fd // We either had a fid and opened it, or we didn't and we're just stuck with our fd
return m_fd; auto rv = m_fd;
return rv;
} }
BeesFileRange BeesFileRange