mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-14 22:31:29 +03:00
AK: Add formatting for infinity and NaN
Without this patch, we would end up printing garbage values when we encountered floating point infinity or NaN values, and also triggered UBSAN with Clang. This added code models `std::format`'s behavior: the sign is added the same way as with normal values and the strings 'nan' and 'inf' are printed.
This commit is contained in:
parent
12dc2c2079
commit
1472f6d986
Notes:
sideshowbarker
2024-07-18 07:15:23 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/1472f6d9865 Pull-request: https://github.com/SerenityOS/serenity/pull/8718 Issue: https://github.com/SerenityOS/serenity/issues/363 Reviewed-by: https://github.com/gunnarbeutner ✅ Reviewed-by: https://github.com/nico
@ -19,6 +19,7 @@
|
||||
# include <Kernel/Process.h>
|
||||
# include <Kernel/Thread.h>
|
||||
#else
|
||||
# include <math.h>
|
||||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
#endif
|
||||
@ -344,6 +345,22 @@ void FormatBuilder::put_f64(
|
||||
StringBuilder string_builder;
|
||||
FormatBuilder format_builder { string_builder };
|
||||
|
||||
if (isnan(value) || isinf(value)) [[unlikely]] {
|
||||
if (value < 0.0)
|
||||
string_builder.append('-');
|
||||
else if (sign_mode == SignMode::Always)
|
||||
string_builder.append('+');
|
||||
else
|
||||
string_builder.append(' ');
|
||||
|
||||
if (isnan(value))
|
||||
string_builder.append(upper_case ? "NAN"sv : "nan"sv);
|
||||
else
|
||||
string_builder.append(upper_case ? "INF"sv : "inf"sv);
|
||||
format_builder.put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
|
||||
return;
|
||||
}
|
||||
|
||||
bool is_negative = value < 0.0;
|
||||
if (is_negative)
|
||||
value = -value;
|
||||
@ -394,6 +411,22 @@ void FormatBuilder::put_f80(
|
||||
StringBuilder string_builder;
|
||||
FormatBuilder format_builder { string_builder };
|
||||
|
||||
if (isnan(value) || isinf(value)) [[unlikely]] {
|
||||
if (value < 0.0l)
|
||||
string_builder.append('-');
|
||||
else if (sign_mode == SignMode::Always)
|
||||
string_builder.append('+');
|
||||
else
|
||||
string_builder.append(' ');
|
||||
|
||||
if (isnan(value))
|
||||
string_builder.append(upper_case ? "NAN"sv : "nan"sv);
|
||||
else
|
||||
string_builder.append(upper_case ? "INF"sv : "inf"sv);
|
||||
format_builder.put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
|
||||
return;
|
||||
}
|
||||
|
||||
bool is_negative = value < 0.0l;
|
||||
if (is_negative)
|
||||
value = -value;
|
||||
|
Loading…
Reference in New Issue
Block a user