mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibWasm: Make the Truncate operator trap on undefined results
This commit is contained in:
parent
1465b11b58
commit
f492e98f19
Notes:
sideshowbarker
2024-07-18 05:03:44 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/f492e98f19c Pull-request: https://github.com/SerenityOS/serenity/pull/9685
1 changed files with 10 additions and 4 deletions
|
@ -259,7 +259,7 @@ struct Floor {
|
|||
};
|
||||
struct Truncate {
|
||||
template<typename Lhs>
|
||||
auto operator()(Lhs lhs) const
|
||||
Result<Lhs, StringView> operator()(Lhs lhs) const
|
||||
{
|
||||
if constexpr (IsSame<Lhs, float>)
|
||||
return truncf(lhs);
|
||||
|
@ -327,10 +327,13 @@ struct CheckedTruncate {
|
|||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
if (NumericLimits<ResultT>::min() <= truncated && static_cast<double>(NumericLimits<ResultT>::max()) >= static_cast<double>(truncated))
|
||||
return static_cast<ResultT>(truncated);
|
||||
// FIXME: This function assumes that all values of ResultT are representable in Lhs
|
||||
// the assumption comes from the fact that this was used exclusively by LibJS,
|
||||
// which only considers values that are all representable in 'double'.
|
||||
if (!AK::is_within_range<ResultT>(truncated))
|
||||
return "Truncation out of range"sv;
|
||||
|
||||
return "Truncation out of range"sv;
|
||||
return static_cast<ResultT>(truncated);
|
||||
}
|
||||
|
||||
static StringView name() { return "truncate.checked"; }
|
||||
|
@ -424,6 +427,9 @@ struct SaturatingTruncate {
|
|||
return NumericLimits<ResultT>::max();
|
||||
}
|
||||
|
||||
// FIXME: This assumes that all values in ResultT are representable in 'double'.
|
||||
// that assumption is not correct, which makes this function yield incorrect values
|
||||
// for 'edge' values of type i64.
|
||||
constexpr auto convert = [](auto truncated_value) {
|
||||
if (truncated_value < NumericLimits<ResultT>::min())
|
||||
return NumericLimits<ResultT>::min();
|
||||
|
|
Loading…
Reference in a new issue