mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +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
Notes:
sideshowbarker
2024-07-17 22:09:47 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/334f5e319c Pull-request: https://github.com/SerenityOS/serenity/pull/24090 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/DanShaders
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;
|
return x;
|
||||||
|
|
||||||
using Extractor = FloatExtractor<decltype(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 extractor;
|
||||||
extractor.d = x;
|
extractor.d = x;
|
||||||
|
|
||||||
|
@ -90,7 +93,8 @@ static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
|
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;
|
auto dead_bits = extractor.mantissa & dead_mask;
|
||||||
extractor.mantissa &= ~dead_mask;
|
extractor.mantissa &= ~dead_mask;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue