Fix potential integer overflow in test::hash<int>

This commit is contained in:
Christian Mazakas
2022-12-12 12:16:49 -08:00
parent 2c1c99407e
commit 47e205487d

View File

@@ -230,18 +230,18 @@ namespace test {
std::size_t operator()(int x) const std::size_t operator()(int x) const
{ {
int result; unsigned result;
switch (type_) { switch (type_) {
case 1: case 1:
result = x; result = static_cast<unsigned>(x);
break; break;
case 2: case 2:
result = x * 7; result = static_cast<unsigned>(x) * 7;
break; break;
default: default:
result = x * 256; result = static_cast<unsigned>(x) * 256;
} }
return static_cast<std::size_t>(result); return result;
} }
friend bool operator==(hash const& x1, hash const& x2) friend bool operator==(hash const& x1, hash const& x2)