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.

llvm-svn: 261585
This commit is contained in:
Duncan P. N. Exon Smith 2016-02-22 22:24:22 +00:00
parent 964b70d559
commit 3a7def09fe
2 changed files with 14 additions and 5 deletions

View File

@ -86,10 +86,10 @@ bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,
// 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(StringRef Filename,
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) {

View File

@ -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();