Skip to content

Commit 9fee338

Browse files
derekmaurocopybara-github
authored andcommitted
hash_generator_testing: Avoid using invalid enum values
Invalid enum values are undefined behavior. This change makes the enum backed by an integer for testing. Our UBSAN tests were missing `-fno-sanitize-recover`, which means UBSAN logs a warning, but the program continues, causing the test not to fail. `-fno-sanitize-recover` will be added once all errors are fixed. PiperOrigin-RevId: 733445015 Change-Id: Ie31fe1d06520d91dfe291b4488fe3d4ca29c72c0
1 parent be68967 commit 9fee338

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

absl/container/internal/hash_generator_testing.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
5050

5151
std::mt19937_64* GetSharedRng();
5252

53-
enum Enum {
53+
enum Enum : uint64_t {
5454
kEnumEmpty,
5555
kEnumDeleted,
5656
};
@@ -80,11 +80,7 @@ struct Generator<Enum> {
8080
Enum operator()() const {
8181
std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
8282
dist;
83-
while (true) {
84-
auto variate = dist(*GetSharedRng());
85-
if (variate != kEnumEmpty && variate != kEnumDeleted)
86-
return static_cast<Enum>(variate);
87-
}
83+
return static_cast<Enum>(dist(*GetSharedRng()));
8884
}
8985
};
9086

@@ -94,11 +90,7 @@ struct Generator<EnumClass> {
9490
std::uniform_int_distribution<
9591
typename std::underlying_type<EnumClass>::type>
9692
dist;
97-
while (true) {
98-
EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
99-
if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
100-
return static_cast<EnumClass>(variate);
101-
}
93+
return static_cast<EnumClass>(dist(*GetSharedRng()));
10294
}
10395
};
10496

0 commit comments

Comments
 (0)