Skip to content

Commit

Permalink
Fix underflow in sizeBucketsPowerAndLocksPower()
Browse files Browse the repository at this point in the history
Summary: If `cacheEntries` is too small, `bucketsPower_` will be less than 10. Since `bucketsPower_` is unsigned, it will underflow if we subtract 10 from it.

Reviewed By: jaesoo-fb

Differential Revision: D50994522

fbshipit-source-id: 55151fdab1982442a706267b3dd3a0bb6a6fd6e7
  • Loading branch information
Alex Schneidman authored and facebook-github-bot committed Nov 6, 2023
1 parent 0238600 commit 90d6313
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cachelib/allocator/ChainedHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ class ChainedHashTable {
}

// 1 lock per 1000 buckets.
locksPower_ = std::max<unsigned int>(1, bucketsPower_ - 10);
locksPower_ =
(bucketsPower_ <= 20) ? (bucketsPower_ / 2) + 1 : bucketsPower_ - 10;
}

unsigned int getBucketsPower() const noexcept { return bucketsPower_; }
Expand Down
4 changes: 4 additions & 0 deletions cachelib/allocator/tests/ChainedHashTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ TEST(ChainedHashTableConfigTest, Size) {

ASSERT_THROW(config.sizeBucketsPowerAndLocksPower(2700000000),
std::invalid_argument);

config.sizeBucketsPowerAndLocksPower(1);
EXPECT_EQ(config.getBucketsPower(), 1);
EXPECT_EQ(config.getLocksPower(), 1);
}

TEST_F(ChainedHashTest, Insert) { testInsert(); }
Expand Down

0 comments on commit 90d6313

Please sign in to comment.