From db8ea92133d6b55d9a5da44d486a945833074107 Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sun, 22 Jan 2017 21:50:21 -0500 Subject: [PATCH] bees: fix further instances of copy-after-unlock bug Before: unique_lock lock(some_mutex); // run lock.~unique_lock() because return // return reference to unprotected heap return foo[bar]; After: unique_lock 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 --- src/bees-context.cc | 12 ++++++++---- src/bees-roots.cc | 6 ++++-- src/bees-types.cc | 6 ++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/bees-context.cc b/src/bees-context.cc index babee3f..1c82bd7 100644 --- a/src/bees-context.cc +++ b/src/bees-context.cc @@ -969,7 +969,8 @@ BeesContext::tmpfile() if (!m_tmpfiles[this_thread::get_id()]) { m_tmpfiles[this_thread::get_id()] = make_shared(shared_from_this()); } - return m_tmpfiles[this_thread::get_id()]; + auto rv = m_tmpfiles[this_thread::get_id()]; + return rv; } shared_ptr @@ -980,7 +981,8 @@ BeesContext::fd_cache() if (!m_fd_cache) { m_fd_cache = make_shared(); } - return m_fd_cache; + auto rv = m_fd_cache; + return rv; } shared_ptr @@ -991,7 +993,8 @@ BeesContext::roots() if (!m_roots) { m_roots = make_shared(shared_from_this()); } - return m_roots; + auto rv = m_roots; + return rv; } shared_ptr @@ -1002,7 +1005,8 @@ BeesContext::hash_table() if (!m_hash_table) { m_hash_table = make_shared(shared_from_this(), "beeshash.dat"); } - return m_hash_table; + auto rv = m_hash_table; + return rv; } void diff --git a/src/bees-roots.cc b/src/bees-roots.cc index 953f2ea..4fba68d 100644 --- a/src/bees-roots.cc +++ b/src/bees-roots.cc @@ -822,7 +822,8 @@ BeesCrawl::peek_front() if (m_extents.empty()) { return BeesFileRange(); } - return *m_extents.begin(); + auto rv = *m_extents.begin(); + return rv; } BeesFileRange @@ -848,7 +849,8 @@ BeesCrawlState BeesCrawl::get_state() { unique_lock lock(m_state_mutex); - return m_state; + auto rv = m_state; + return rv; } void diff --git a/src/bees-types.cc b/src/bees-types.cc index 95ad945..54a4959 100644 --- a/src/bees-types.cc +++ b/src/bees-types.cc @@ -286,7 +286,8 @@ Fd BeesFileRange::fd() const { unique_lock lock(s_mutex); - return m_fd; + auto rv = m_fd; + return rv; } Fd @@ -310,7 +311,8 @@ BeesFileRange::fd(const shared_ptr &ctx) const } } // 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