llvm.org GIT mirror llvm / 2a79116
Fix PR13148, an inf-loop in StringMap. StringMap suffered from the same bug as DenseMap: when you explicitly construct it with a small number of buckets, you can arrange for the tombstone-based growth path to be followed when the number of buckets was less than '8'. In that case, even with a full map, it would compare '0' as not less than '0', and refuse to grow the table, leading to inf-loops trying to find an empty bucket on the next insertion. The fix is very simple: use '<=' as the comparison. The same fix was applied to DenseMap as well during its recent refactoring. Thanks to Alex Bolz for the great report and test case. =] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158725 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
2 changed file(s) with 23 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
188188 // grow/rehash the table.
189189 if (NumItems*4 > NumBuckets*3) {
190190 NewSize = NumBuckets*2;
191 } else if (NumBuckets-(NumItems+NumTombstones) < NumBuckets/8) {
191 } else if (NumBuckets-(NumItems+NumTombstones) <= NumBuckets/8) {
192192 NewSize = NumBuckets;
193193 } else {
194194 return;
133133 assertSingleItemMap();
134134 }
135135
136 TEST_F(StringMapTest, SmallFullMapTest) {
137 // StringMap has a tricky corner case when the map is small (<8 buckets) and
138 // it fills up through a balanced pattern of inserts and erases. This can
139 // lead to inf-loops in some cases (PR13148) so we test it explicitly here.
140 llvm::StringMap Map(2);
141
142 Map["eins"] = 1;
143 Map["zwei"] = 2;
144 Map["drei"] = 3;
145 Map.erase("drei");
146 Map.erase("eins");
147 Map["veir"] = 4;
148 Map["funf"] = 5;
149
150 EXPECT_EQ(3u, Map.size());
151 EXPECT_EQ(0, Map.lookup("eins"));
152 EXPECT_EQ(2, Map.lookup("zwei"));
153 EXPECT_EQ(0, Map.lookup("drei"));
154 EXPECT_EQ(4, Map.lookup("veir"));
155 EXPECT_EQ(5, Map.lookup("funf"));
156 }
157
136158 // A more complex iteration test.
137159 TEST_F(StringMapTest, IterationTest) {
138160 bool visited[100];