From f0e4cac7ebdee07639bd1fc3eadf204f45868556 Mon Sep 17 00:00:00 2001 From: Stuart Hastings Date: Fri, 1 May 2009 20:47:53 +0000 Subject: [PATCH] Prevent looping when DenseSet is abused. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70572 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/DenseMap.h | 2 +- unittests/ADT/DenseSetTest.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 unittests/ADT/DenseSetTest.cpp diff --git a/include/llvm/ADT/DenseMap.h b/include/llvm/ADT/DenseMap.h index 07995d7796c..e18be8963d4 100644 --- a/include/llvm/ADT/DenseMap.h +++ b/include/llvm/ADT/DenseMap.h @@ -346,12 +346,12 @@ private: // probe almost the entire table until it found the empty bucket. If the // table completely filled with tombstones, no lookup would ever succeed, // causing infinite loops in lookup. + ++NumEntries; if (NumEntries*4 >= NumBuckets*3 || NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) { this->grow(NumBuckets * 2); LookupBucketFor(Key, TheBucket); } - ++NumEntries; // If we are writing over a tombstone, remember this. if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey())) diff --git a/unittests/ADT/DenseSetTest.cpp b/unittests/ADT/DenseSetTest.cpp new file mode 100644 index 00000000000..7a35f521a19 --- /dev/null +++ b/unittests/ADT/DenseSetTest.cpp @@ -0,0 +1,30 @@ +//===- llvm/unittest/ADT/DenseSetTest.cpp - DenseSet unit tests --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include + +using namespace llvm; + +namespace { + +// Test fixture +class DenseSetTest : public testing::Test { +}; + +// Test hashing with a set of only two entries. +TEST_F(DenseSetTest, DoubleEntrySetTest) { + llvm::DenseSet set(2); + set.insert(0); + set.insert(1); + // Original failure was an infinite loop in this call: + EXPECT_EQ(0, set.count(2)); +} + +} -- 2.34.1