mirror of
				https://github.com/Zygo/bees.git
				synced 2025-11-03 19:50:34 +01:00 
			
		
		
		
	crucible: 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:
		@@ -161,7 +161,10 @@ namespace crucible {
 | 
			
		||||
	LockSet<T>::copy()
 | 
			
		||||
	{
 | 
			
		||||
		unique_lock<mutex> lock(m_mutex);
 | 
			
		||||
		return m_set;
 | 
			
		||||
		// Make temporary copy of set while protected by mutex
 | 
			
		||||
		auto rv = m_set;
 | 
			
		||||
		// Return temporary copy after releasing lock
 | 
			
		||||
		return rv;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	template <class T>
 | 
			
		||||
 
 | 
			
		||||
@@ -332,7 +332,9 @@ namespace crucible {
 | 
			
		||||
	ResourceHandle<Key, Resource>::get_resource_ptr() const
 | 
			
		||||
	{
 | 
			
		||||
		unique_lock<mutex> lock(s_ptr_mutex);
 | 
			
		||||
		return m_ptr;
 | 
			
		||||
		// Make isolated copy of pointer with lock held, and return the copy
 | 
			
		||||
		auto rv = m_ptr;
 | 
			
		||||
		return rv;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	template <class Key, class Resource>
 | 
			
		||||
@@ -343,7 +345,9 @@ namespace crucible {
 | 
			
		||||
		if (!m_ptr) {
 | 
			
		||||
			THROW_ERROR(out_of_range, __PRETTY_FUNCTION__ << " called on null Resource");
 | 
			
		||||
		}
 | 
			
		||||
		return m_ptr;
 | 
			
		||||
		// Make isolated copy of pointer with lock held, and return the copy
 | 
			
		||||
		auto rv = m_ptr;
 | 
			
		||||
		return rv;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	template <class Key, class Resource>
 | 
			
		||||
 
 | 
			
		||||
@@ -124,7 +124,9 @@ namespace crucible {
 | 
			
		||||
		if (m_set.empty()) {
 | 
			
		||||
			return key_type();
 | 
			
		||||
		} else {
 | 
			
		||||
			return *m_set.begin();
 | 
			
		||||
			// Make copy with lock held
 | 
			
		||||
			auto rv = *m_set.begin();
 | 
			
		||||
			return rv;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -149,7 +151,8 @@ namespace crucible {
 | 
			
		||||
	WorkQueue<Task>::copy()
 | 
			
		||||
	{
 | 
			
		||||
		unique_lock<mutex> lock(m_mutex);
 | 
			
		||||
		return m_set;
 | 
			
		||||
		auto rv = m_set;
 | 
			
		||||
		return rv;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	template <class Task>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user