mirror of
https://github.com/SerenityOS/serenity
synced 2024-11-05 17:46:52 +00:00
LibC: Prevent undefined shift in internal_to_integer
New tests will hit a dead bit count of 64, leading to an undefined shift.
This commit is contained in:
parent
8443d0a74d
commit
334f5e319c
1 changed files with 5 additions and 1 deletions
|
@ -68,6 +68,9 @@ static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
|
|||
return x;
|
||||
|
||||
using Extractor = FloatExtractor<decltype(x)>;
|
||||
// Most component types are larger than int.
|
||||
constexpr auto zero = static_cast<Extractor::ComponentType>(0);
|
||||
constexpr auto one = static_cast<Extractor::ComponentType>(1);
|
||||
Extractor extractor;
|
||||
extractor.d = x;
|
||||
|
||||
|
@ -90,7 +93,8 @@ static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
|
|||
return x;
|
||||
|
||||
auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
|
||||
auto dead_mask = (1ull << dead_bitcount) - 1;
|
||||
// Avoid shifting by the integer type's size since that's UB.
|
||||
auto dead_mask = dead_bitcount == sizeof(typename Extractor::ComponentType) * 8 ? ~zero : (one << dead_bitcount) - 1;
|
||||
auto dead_bits = extractor.mantissa & dead_mask;
|
||||
extractor.mantissa &= ~dead_mask;
|
||||
|
||||
|
|
Loading…
Reference in a new issue