From 8e0e69a404bc516a254ebdbefc70f33d6e1aec21 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 00:06:04 -0600 Subject: [PATCH 1/2] fix: CacheMultiMap use-after-free on same-key eviction and broken copy Insert looked up the key's index entry before pruning. When the cache was full and the evicted item was the last one for that same key, PruneLast erased the entry and Insert then wrote through the dangling reference. The copy constructor and assignment operator took a CacheMap, so they never acted as copy operations and the implicit memberwise ones were used instead. A copy's index then pointed into the source's list. Co-Authored-By: Claude Fable 5.2 --- src/cachemultimap.h | 18 ++++++++---------- src/test/cachemultimap_tests.cpp | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/cachemultimap.h b/src/cachemultimap.h index 88f3f12c2bea..f58c2bba4466 100644 --- a/src/cachemultimap.h +++ b/src/cachemultimap.h @@ -57,7 +57,7 @@ class CacheMultiMap mapIndex() {} - explicit CacheMultiMap(const CacheMap& other) + CacheMultiMap(const CacheMultiMap& other) : nMaxSize(other.nMaxSize), listItems(other.listItems), mapIndex() @@ -86,22 +86,17 @@ class CacheMultiMap bool Insert(const K& key, const V& value) { - map_it mit = mapIndex.find(key); - if(mit == mapIndex.end()) { - mit = mapIndex.emplace(key, it_map_t()).first; - } - it_map_t& mapIt = mit->second; - - if(mapIt.count(value) > 0) { + if (map_cit mit = mapIndex.find(key); mit != mapIndex.end() && mit->second.count(value) > 0) { // Don't insert duplicates return false; } + // Pruning may erase this key's entry in mapIndex, so look it up only afterwards if(listItems.size() == nMaxSize) { PruneLast(); } listItems.push_front(item_t(key, value)); - mapIt.emplace(value, listItems.begin()); + mapIndex[key].emplace(value, listItems.begin()); return true; } @@ -184,8 +179,11 @@ class CacheMultiMap return listItems; } - CacheMap& operator=(const CacheMap& other) + CacheMultiMap& operator=(const CacheMultiMap& other) { + if (this == &other) { + return *this; + } nMaxSize = other.nMaxSize; listItems = other.listItems; RebuildIndex(); diff --git a/src/test/cachemultimap_tests.cpp b/src/test/cachemultimap_tests.cpp index 46712b831b98..0cb52149c44a 100644 --- a/src/test/cachemultimap_tests.cpp +++ b/src/test/cachemultimap_tests.cpp @@ -164,6 +164,28 @@ BOOST_AUTO_TEST_CASE(cachemultimap_test) CacheMultiMap mapTest4; mapTest4 = cmmapTest1; BOOST_CHECK(Compare(cmmapTest1, mapTest4)); + + // copies must keep working after the source's storage is gone + cmmapTest1.Clear(); + for (auto* copy : {&cmmapTest3, &mapTest4}) { + std::vector vals; + BOOST_CHECK(copy->GetAll(5, vals)); + BOOST_CHECK(vals == std::vector({1, 2, 4})); + copy->Erase(5); + BOOST_CHECK(!copy->HasKey(5)); + } +} + +BOOST_AUTO_TEST_CASE(same_key_eviction_at_capacity) +{ + CacheMultiMap cache(1); + BOOST_REQUIRE(cache.Insert(42, 1)); + BOOST_CHECK(!cache.Insert(42, 1)); + BOOST_REQUIRE(cache.Insert(42, 2)); + BOOST_CHECK_EQUAL(cache.GetSize(), 1); + int value = 0; + BOOST_CHECK(cache.Get(42, value)); + BOOST_CHECK_EQUAL(value, 2); } BOOST_AUTO_TEST_SUITE_END() From da98191ce6338f92250aeedf5298fd507c6d4ab5 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 00:06:04 -0600 Subject: [PATCH 2/2] fix: enforce the capacity of CacheMap and CacheMultiMap A cache with capacity zero, which is what the default constructor gives, accepted inserts and grew without bound, and SetMaxSize left entries above a reduced bound in place. Reject inserts at zero capacity, prune in SetMaxSize, and let Insert prune until there is room instead of evicting exactly one entry. No production cache is built with capacity zero or resized, so this changes no node behaviour. Co-Authored-By: Claude Fable 5.2 --- src/cachemap.h | 8 +++++++- src/cachemultimap.h | 8 +++++++- src/test/cachemap_tests.cpp | 29 +++++++++++++++++++++++++++++ src/test/cachemultimap_tests.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/cachemap.h b/src/cachemap.h index c99ad917983c..c68608fb2bab 100644 --- a/src/cachemap.h +++ b/src/cachemap.h @@ -89,6 +89,9 @@ class CacheMap void SetMaxSize(size_type nMaxSizeIn) { nMaxSize = nMaxSizeIn; + while (listItems.size() > nMaxSize) { + PruneLast(); + } } size_type GetMaxSize() const { @@ -101,10 +104,13 @@ class CacheMap bool Insert(const K& key, const V& value) { + if (nMaxSize == 0) { + return false; + } if(mapIndex.find(key) != mapIndex.end()) { return false; } - if(listItems.size() == nMaxSize) { + while (listItems.size() >= nMaxSize) { PruneLast(); } listItems.push_front(item_t(key, value)); diff --git a/src/cachemultimap.h b/src/cachemultimap.h index f58c2bba4466..22d77691722d 100644 --- a/src/cachemultimap.h +++ b/src/cachemultimap.h @@ -74,6 +74,9 @@ class CacheMultiMap void SetMaxSize(size_type nMaxSizeIn) { nMaxSize = nMaxSizeIn; + while (listItems.size() > nMaxSize) { + PruneLast(); + } } size_type GetMaxSize() const { @@ -86,13 +89,16 @@ class CacheMultiMap bool Insert(const K& key, const V& value) { + if (nMaxSize == 0) { + return false; + } if (map_cit mit = mapIndex.find(key); mit != mapIndex.end() && mit->second.count(value) > 0) { // Don't insert duplicates return false; } // Pruning may erase this key's entry in mapIndex, so look it up only afterwards - if(listItems.size() == nMaxSize) { + while (listItems.size() >= nMaxSize) { PruneLast(); } listItems.push_front(item_t(key, value)); diff --git a/src/test/cachemap_tests.cpp b/src/test/cachemap_tests.cpp index 70b67069a8b3..dd84cd09c00a 100644 --- a/src/test/cachemap_tests.cpp +++ b/src/test/cachemap_tests.cpp @@ -135,4 +135,33 @@ BOOST_AUTO_TEST_CASE(cachemap_test) BOOST_CHECK(Compare(cmapTest1, mapTest4)); } +BOOST_AUTO_TEST_CASE(capacity_limits) +{ + CacheMap cache(0); + BOOST_CHECK(!cache.Insert(1, 1)); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(1)); + + cache.Clear(); + cache.SetMaxSize(10); + for (int i = 0; i < 10; ++i) { + BOOST_REQUIRE(cache.Insert(i, i)); + } + cache.SetMaxSize(5); + BOOST_CHECK_EQUAL(cache.GetSize(), 5); + for (int i = 0; i < 10; ++i) { + int value = -1; + if (i < 5) { + BOOST_CHECK(!cache.Get(i, value)); + } else { + BOOST_CHECK(cache.Get(i, value)); + BOOST_CHECK_EQUAL(value, i); + } + } + cache.SetMaxSize(0); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(9)); + BOOST_CHECK(!cache.Insert(1, 1)); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/cachemultimap_tests.cpp b/src/test/cachemultimap_tests.cpp index 0cb52149c44a..31ad94a94dab 100644 --- a/src/test/cachemultimap_tests.cpp +++ b/src/test/cachemultimap_tests.cpp @@ -188,4 +188,35 @@ BOOST_AUTO_TEST_CASE(same_key_eviction_at_capacity) BOOST_CHECK_EQUAL(value, 2); } +BOOST_AUTO_TEST_CASE(capacity_limits) +{ + CacheMultiMap cache(0); + BOOST_CHECK(!cache.Insert(1, 1)); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(1)); + + cache.Clear(); + cache.SetMaxSize(10); + for (int i = 0; i < 10; ++i) { + BOOST_REQUIRE(cache.Insert(i / 2, i)); + } + cache.SetMaxSize(5); + BOOST_CHECK_EQUAL(cache.GetSize(), 5); + for (int i = 0; i < 5; ++i) { + std::vector values; + const bool found = cache.GetAll(i, values); + if (i < 2) { + BOOST_CHECK(!found); + } else { + BOOST_CHECK(found); + const std::vector expected = i == 2 ? std::vector{5} : std::vector{i * 2, i * 2 + 1}; + BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end()); + } + } + cache.SetMaxSize(0); + BOOST_CHECK_EQUAL(cache.GetSize(), 0); + BOOST_CHECK(!cache.HasKey(4)); + BOOST_CHECK(!cache.Insert(1, 1)); +} + BOOST_AUTO_TEST_SUITE_END()