Prevent iterator invalidation in CapacityCache 95/253495/3
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 12 Feb 2021 09:00:35 +0000 (10:00 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 12 Feb 2021 09:31:23 +0000 (09:31 +0000)
If resultIt happens to point to the element that gets evicted, it will
be invalidated. Perform lookup after possible eviction.

Change-Id: I4d536c6779494f85077a247e3356374346fa35b2

src/client-common/cache/CapacityCache.cpp

index ba433757fecec62c0a89d03911197ec403352910..644b99353ff199aeae70c152b44611992a67b7a1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2014-2021 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * This file is licensed under the terms of MIT License or the Apache License
  * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
@@ -130,7 +130,6 @@ int CapacityCache::update(const ClientSession &session,
 
     if (m_capacity > 0) {
         std::string cacheKey = keyToString(key);
-        auto resultIt = m_keyValue.find(cacheKey);
         if (plugin->isCacheable(session, storedResult)) {
             LOGD("Entry cacheable");
             if (m_keyValue.size() == m_capacity) {
@@ -139,6 +138,7 @@ int CapacityCache::update(const ClientSession &session,
             }
 
             //Move value usage to front
+            auto resultIt = m_keyValue.find(cacheKey);
             if (resultIt != m_keyValue.end()) {
                 auto usageIt = std::get<2>(resultIt->second);
                 m_keyUsage.splice(m_keyUsage.begin(), m_keyUsage, usageIt);
@@ -149,6 +149,7 @@ int CapacityCache::update(const ClientSession &session,
             m_keyValue[cacheKey] = std::make_tuple(storedResult, session, m_keyUsage.begin());
         } else {
             //Remove element
+            auto resultIt = m_keyValue.find(cacheKey);
             if (resultIt != m_keyValue.end()) {
                 auto usageIt = std::get<2>(resultIt->second);
                 m_keyUsage.erase(usageIt);