bit_util: Do nothing in RotateRight if the rotation amount is zero

Without this sanitizing it's possible to perform a shift with a shift
amount that's the same size as the type being shifted. This actually
occurs when decoding ORR variants.

We could get fancier here and make this branchless, but we don't
really use RotateRight in any performance intensive areas.
This commit is contained in:
Lioncash 2018-03-21 13:51:47 -04:00 committed by MerryMage
parent 9128988dc3
commit ade595e377

View file

@ -130,6 +130,11 @@ inline T Replicate(T value, size_t element_size) {
template <typename T> template <typename T>
inline T RotateRight(T value, size_t amount) { inline T RotateRight(T value, size_t amount) {
amount %= BitSize<T>(); amount %= BitSize<T>();
if (amount == 0) {
return value;
}
auto x = static_cast<std::make_unsigned_t<T>>(value); auto x = static_cast<std::make_unsigned_t<T>>(value);
return static_cast<T>((x >> amount) | (x << (BitSize<T>() - amount))); return static_cast<T>((x >> amount) | (x << (BitSize<T>() - amount)));
} }