Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RaiiMapOfListElement: fix invalid reference due to rehashing instability #15719

Merged
merged 2 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions source/common/common/cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ template <class Key, class Value> class RaiiMapOfListElement {

template <typename ConvertibleToKey>
RaiiMapOfListElement(MapOfList& map, const ConvertibleToKey& key, Value value)
: map_(map), list_(map_.try_emplace(key).first->second), key_(key), cancelled_(false) {
it_ = list_.emplace(list_.begin(), value);
: map_(map), key_(key), cancelled_(false) {
// The list reference itself cannot be saved because it is not stable in the event of a
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: an alternative fix is to use absl::node_hash_map. I went ahead with this fix because it doesn't require usage site changes, and abseil generally recommends using flat_hash_map, over node_hash_map unless it's necessary.

// absl::flat_hash_map rehash.
std::list<Value>& list = map_.try_emplace(key).first->second;
it_ = list.emplace(list.begin(), value);
}

virtual ~RaiiMapOfListElement() {
Expand All @@ -79,16 +82,18 @@ template <class Key, class Value> class RaiiMapOfListElement {
private:
void erase() {
ASSERT(!cancelled_);
list_.erase(it_);
if (list_.empty()) {
auto list_it = map_.find(key_);
ASSERT(list_it != map_.end());

list_it->second.erase(it_);
if (list_it->second.empty()) {
map_.erase(key_);
}
cancelled_ = true;
}

MapOfList& map_;
std::list<Value>& list_;
// Because of absl::flat_hash_map iterator instability we have to keep a copy of the key
// Because of absl::flat_hash_map iterator instability we have to keep a copy of the key.
const Key key_;
typename MapOfList::mapped_type::iterator it_;
bool cancelled_;
Expand Down
20 changes: 20 additions & 0 deletions test/common/common/cleanup_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,24 @@ TEST(RaiiMapOfListElement, MultipleEntriesSameKey) {
EXPECT_EQ(map.size(), 0);
}

TEST(RaiiMapOfListElement, DeleteAfterMapRehash) {
absl::flat_hash_map<int, std::list<int>> map;
std::list<RaiiMapOfListElement<int, int>> list;
// According to https://abseil.io/docs/cpp/guides/container the max load factor on
// absl::flat_hash_map is 87.5%. Using bucket_count and multiplying by 2 should give us enough
// head room to cause rehashing.
int rehash_limit = (map.bucket_count() == 0 ? 1 : map.bucket_count()) * 2;

for (int i = 0; i <= rehash_limit; i++) {
list.emplace_back(map, i, i);
EXPECT_EQ(map.size(), i + 1);
auto it = map.find(i);
ASSERT_NE(map.end(), it);
EXPECT_EQ(it->second.size(), 1);
}

list.clear();
EXPECT_EQ(map.size(), 0);
}

} // namespace Envoy