#include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SwapByteOrder.h"
#include <cstdio>
#include <memory>
using namespace clang;
if (Header->Reserved != 0)
return false;
+ // Check the number of buckets.
+ auto NumBuckets = NeedsByteSwap
+ ? llvm::sys::getSwappedBytes(Header->NumBuckets)
+ : Header->NumBuckets;
+
+ // If the number of buckets is not a power of two, the headermap is corrupt.
+ if (NumBuckets & (NumBuckets - 1))
+ return false;
+
// Okay, everything looks good.
return true;
}
const HMapHeader &Hdr = getHeader();
unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
- // If the number of buckets is not a power of two, the headermap is corrupt.
- // Don't probe infinitely.
- if (NumBuckets & (NumBuckets-1))
- return StringRef();
+ // Don't probe infinitely. This should be checked before constructing.
+ assert(!(NumBuckets & (NumBuckets - 1)) && "Expected power of 2");
// Linearly probe the hash table.
for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
ASSERT_TRUE(NeedsSwap);
}
+TEST(HeaderMapTest, checkHeader3Buckets) {
+ MapFile<3, 1> File;
+ ASSERT_EQ(3 * sizeof(HMapBucket), sizeof(File.Buckets));
+
+ File.init();
+ bool NeedsSwap;
+ ASSERT_FALSE(HeaderMapImpl::checkHeader(*File.getBuffer(), NeedsSwap));
+}
+
} // end namespace