Fix a buffer overrun detected by AddressSanitizer.

llvm-svn: 197647
diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h
index 8fb538f..6f69aba 100644
--- a/llvm/include/llvm/ADT/BitVector.h
+++ b/llvm/include/llvm/ADT/BitVector.h
@@ -267,7 +267,8 @@
       Bits[I / BITWORD_SIZE] = ~0UL;
 
     BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
-    Bits[I / BITWORD_SIZE] |= PostfixMask;
+    if (I < E)
+      Bits[I / BITWORD_SIZE] |= PostfixMask;
 
     return *this;
   }
@@ -305,7 +306,8 @@
       Bits[I / BITWORD_SIZE] = 0UL;
 
     BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
-    Bits[I / BITWORD_SIZE] &= ~PostfixMask;
+    if (I < E)
+      Bits[I / BITWORD_SIZE] &= ~PostfixMask;
 
     return *this;
   }
diff --git a/llvm/unittests/ADT/BitVectorTest.cpp b/llvm/unittests/ADT/BitVectorTest.cpp
index d7cde89..3deaff0 100644
--- a/llvm/unittests/ADT/BitVectorTest.cpp
+++ b/llvm/unittests/ADT/BitVectorTest.cpp
@@ -356,6 +356,12 @@
   EXPECT_TRUE( E.test(1));
   EXPECT_TRUE( E.test(32));
   EXPECT_FALSE(E.test(33));
+
+  TypeParam BufferOverrun;
+  unsigned size = sizeof(unsigned long) * 8;
+  BufferOverrun.resize(size);
+  BufferOverrun.reset(0, size);
+  BufferOverrun.set(0, size);
 }
 
 TYPED_TEST(BitVectorTest, CompoundTestReset) {