Skip to content

Commit 7ece455

Browse files
ezbrcopybara-github
authored andcommitted
Disallow using a hash function whose return type is smaller than size_t.
Swisstable relies on the hash having good entropy in both the high and low bits. PiperOrigin-RevId: 786854836 Change-Id: I6fc7041534984a0e937a11f50a2b478390a1a509
1 parent 57abc0e commit 7ece455

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

absl/container/internal/raw_hash_set.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,13 @@ class raw_hash_set {
19151915
auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k));
19161916
auto KeyTypeCanBeEq(const Eq& eq, const key_type& k) -> decltype(eq(k, k));
19171917

1918+
// Try to be helpful when the hasher returns an unreasonable type.
1919+
using key_hash_result =
1920+
absl::remove_cvref_t<decltype(std::declval<const Hash&>()(
1921+
std::declval<const key_type&>()))>;
1922+
static_assert(sizeof(key_hash_result) >= sizeof(size_t),
1923+
"`Hash::operator()` should return a `size_t`");
1924+
19181925
using AllocTraits = absl::allocator_traits<allocator_type>;
19191926
using SlotAlloc = typename absl::allocator_traits<
19201927
allocator_type>::template rebind_alloc<slot_type>;

0 commit comments

Comments
 (0)