From c58e5cd75bae611dbb46fcaeba5ebbadab5f3e82 Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sun, 22 Jan 2017 12:07:10 -0500 Subject: [PATCH] crucible: cache: construct return value before releasing lock If we release the lock first (and C++ destructor order says we do), then the return value will be constructed from data living in an unprotected container object. That data might be destroyed before we get to the copy constructor for the return value. Make a temporary copy of the return value that won't be destroyed by any other thread, then unlock the mutex, then return the copy object. Signed-off-by: Zygo Blaxell --- include/crucible/cache.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/crucible/cache.h b/include/crucible/cache.h index dac2a8a..60d2a7f 100644 --- a/include/crucible/cache.h +++ b/include/crucible/cache.h @@ -154,7 +154,9 @@ namespace crucible { if (!inserted) { found->second.first = m_ctr++; } - return found->second.second; + // Make copy before releasing lock + auto rv = found->second.second; + return rv; } template