Skip to content

Commit

Permalink
Fix SmallDenseMap assignment operator.
Browse files Browse the repository at this point in the history
Self assignment would lead to buckets of garbage, causing quadratic probing to hang.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214790 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
atrick committed Aug 4, 2014
1 parent 2c68cde commit e85047e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ class DenseMapBase {

template <typename OtherBaseT>
void copyFrom(const DenseMapBase<OtherBaseT, KeyT, ValueT, KeyInfoT>& other) {
assert(&other != this);
assert(getNumBuckets() == other.getNumBuckets());

setNumEntries(other.getNumEntries());
Expand Down Expand Up @@ -574,7 +575,8 @@ class DenseMap
}

DenseMap& operator=(const DenseMap& other) {
copyFrom(other);
if (&other != this)
copyFrom(other);
return *this;
}

Expand Down Expand Up @@ -799,7 +801,8 @@ class SmallDenseMap
}

SmallDenseMap& operator=(const SmallDenseMap& other) {
copyFrom(other);
if (&other != this)
copyFrom(other);
return *this;
}

Expand Down
5 changes: 5 additions & 0 deletions unittests/ADT/DenseMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ TYPED_TEST(DenseMapTest, AssignmentTest) {

EXPECT_EQ(1u, copyMap.size());
EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);

// test self-assignment.
copyMap = copyMap;
EXPECT_EQ(1u, copyMap.size());
EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
}

// Test swap method
Expand Down

0 comments on commit e85047e

Please sign in to comment.