r261585 - Lex: Check for 0 buckets on header map construction

Duncan P. N. Exon Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 22 14:24:23 PST 2016


Author: dexonsmith
Date: Mon Feb 22 16:24:22 2016
New Revision: 261585

URL: http://llvm.org/viewvc/llvm-project?rev=261585&view=rev
Log:
Lex: Check for 0 buckets on header map construction

Switch to using `isPowerOf2_32()` to check whether the buckets are a
power of two, and as a side benefit reject loading a header map with no
buckets.  This is a follow-up to r261448.

Modified:
    cfe/trunk/lib/Lex/HeaderMap.cpp
    cfe/trunk/unittests/Lex/HeaderMapTest.cpp

Modified: cfe/trunk/lib/Lex/HeaderMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/HeaderMap.cpp?rev=261585&r1=261584&r2=261585&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/HeaderMap.cpp (original)
+++ cfe/trunk/lib/Lex/HeaderMap.cpp Mon Feb 22 16:24:22 2016
@@ -86,10 +86,10 @@ bool HeaderMapImpl::checkHeader(const ll
 
   // Check the number of buckets.  It should be a power of two, and there
   // should be enough space in the file for all of them.
-  auto NumBuckets = NeedsByteSwap
-                        ? llvm::sys::getSwappedBytes(Header->NumBuckets)
-                        : Header->NumBuckets;
-  if (NumBuckets & (NumBuckets - 1))
+  uint32_t NumBuckets = NeedsByteSwap
+                            ? llvm::sys::getSwappedBytes(Header->NumBuckets)
+                            : Header->NumBuckets;
+  if (!llvm::isPowerOf2_32(NumBuckets))
     return false;
   if (File.getBufferSize() <
       sizeof(HMapHeader) + sizeof(HMapBucket) * NumBuckets)
@@ -208,7 +208,7 @@ StringRef HeaderMapImpl::lookupFilename(
   unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
 
   // Don't probe infinitely.  This should be checked before constructing.
-  assert(!(NumBuckets & (NumBuckets - 1)) && "Expected power of 2");
+  assert(llvm::isPowerOf2_32(NumBuckets) && "Expected power of 2");
 
   // Linearly probe the hash table.
   for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {

Modified: cfe/trunk/unittests/Lex/HeaderMapTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Lex/HeaderMapTest.cpp?rev=261585&r1=261584&r2=261585&view=diff
==============================================================================
--- cfe/trunk/unittests/Lex/HeaderMapTest.cpp (original)
+++ cfe/trunk/unittests/Lex/HeaderMapTest.cpp Mon Feb 22 16:24:22 2016
@@ -143,6 +143,15 @@ TEST(HeaderMapTest, checkHeader3Buckets)
   ASSERT_FALSE(HeaderMapImpl::checkHeader(*File.getBuffer(), NeedsSwap));
 }
 
+TEST(HeaderMapTest, checkHeader0Buckets) {
+  // Create with 1 bucket to avoid 0-sized arrays.
+  MapFile<1, 1> File;
+  File.init();
+  File.Header.NumBuckets = 0;
+  bool NeedsSwap;
+  ASSERT_FALSE(HeaderMapImpl::checkHeader(*File.getBuffer(), NeedsSwap));
+}
+
 TEST(HeaderMapTest, checkHeaderNotEnoughBuckets) {
   MapFile<1, 1> File;
   File.init();




More information about the cfe-commits mailing list