Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential integer overflow in hash container create/resize #1812

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

module(
name = "abseil-cpp",
version = "20240116.2",
version = "20240116.3",
compatibility_level = 1,
)

Expand Down
2 changes: 1 addition & 1 deletion absl/base/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
// LTS releases can be obtained from
// https://github.com/abseil/abseil-cpp/releases.
#define ABSL_LTS_RELEASE_VERSION 20240116
#define ABSL_LTS_RELEASE_PATCH_LEVEL 2
#define ABSL_LTS_RELEASE_PATCH_LEVEL 3

// Helper macro to convert a CPP variable to a string literal.
#define ABSL_INTERNAL_DO_TOKEN_STR(x) #x
Expand Down
15 changes: 14 additions & 1 deletion absl/container/internal/raw_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,12 @@ inline size_t NormalizeCapacity(size_t n) {
return n ? ~size_t{} >> countl_zero(n) : 1;
}

template <size_t kSlotSize>
size_t MaxValidCapacity() {
return NormalizeCapacity((std::numeric_limits<size_t>::max)() / 4 /
kSlotSize);
}

// General notes on capacity/growth methods below:
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
// average of two empty slots per group.
Expand Down Expand Up @@ -2093,6 +2099,8 @@ class raw_hash_set {
const allocator_type& alloc = allocator_type())
: settings_(CommonFields{}, hash, eq, alloc) {
if (bucket_count) {
ABSL_RAW_CHECK(bucket_count <= MaxValidCapacity<sizeof(slot_type)>(),
"Hash table size overflow");
resize(NormalizeCapacity(bucket_count));
}
}
Expand Down Expand Up @@ -2286,7 +2294,9 @@ class raw_hash_set {
bool empty() const { return !size(); }
size_t size() const { return common().size(); }
size_t capacity() const { return common().capacity(); }
size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }
size_t max_size() const {
return CapacityToGrowth(MaxValidCapacity<sizeof(slot_type)>());
}

ABSL_ATTRIBUTE_REINITIALIZES void clear() {
// Iterating over this container is O(bucket_count()). When bucket_count()
Expand Down Expand Up @@ -2624,6 +2634,8 @@ class raw_hash_set {
auto m = NormalizeCapacity(n | GrowthToLowerboundCapacity(size()));
// n == 0 unconditionally rehashes as per the standard.
if (n == 0 || m > capacity()) {
ABSL_RAW_CHECK(m <= MaxValidCapacity<sizeof(slot_type)>(),
"Hash table size overflow");
resize(m);

// This is after resize, to ensure that we have completed the allocation
Expand All @@ -2634,6 +2646,7 @@ class raw_hash_set {

void reserve(size_t n) {
if (n > size() + growth_left()) {
ABSL_RAW_CHECK(n <= max_size(), "Hash table size overflow");
size_t m = GrowthToLowerboundCapacity(n);
resize(NormalizeCapacity(m));

Expand Down
8 changes: 8 additions & 0 deletions absl/container/internal/raw_hash_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,14 @@ TEST(Table, CountedHash) {
}
}

TEST(Table, MaxSizeOverflow) {
size_t overflow = (std::numeric_limits<size_t>::max)();
EXPECT_DEATH_IF_SUPPORTED(IntTable t(overflow), "Hash table size overflow");
IntTable t;
EXPECT_DEATH_IF_SUPPORTED(t.reserve(overflow), "Hash table size overflow");
EXPECT_DEATH_IF_SUPPORTED(t.rehash(overflow), "Hash table size overflow");
}

} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
Expand Down