Skip to content

Commit

Permalink
<format>: crash when formatting a floating-point value with #/L and…
Browse files Browse the repository at this point in the history
… a large precision (#4907)
  • Loading branch information
Andor233 authored Aug 25, 2024
1 parent 138e116 commit d4b84bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -3095,7 +3095,7 @@ _NODISCARD _OutputIt _Fmt_write(
switch (_Format) {
case chars_format::hex:
case chars_format::scientific:
if (_Extra_precision != 0) {
if (_Extra_precision != 0 && !_Specs._Alt && !_Specs._Localized) {
// Trailing zeroes are in front of the exponent
while (*--_Exponent_start != _Exponent) {
}
Expand Down
22 changes: 22 additions & 0 deletions tests/std/tests/P0645R10_text_formatting_formatting/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,26 @@ void test_gh_4319() {
assert(format(STR("{:}"), 12345678.0) == STR("12345678"));
}

// GH-4320: <format>: crash when formatting a floating-point value with #/L and a large precision
template <class charT>
void test_gh_4320() {
assert(
format(STR("{:#.1075e}"), 1.0)
== STR(
"1."
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000e+00"));
}

void test() {
test_simple_formatting<char>();
test_simple_formatting<wchar_t>();
Expand Down Expand Up @@ -1604,6 +1624,8 @@ void test() {

test_gh_4319<char>();
test_gh_4319<wchar_t>();
test_gh_4320<char>();
test_gh_4320<wchar_t>();
}

int main() {
Expand Down

0 comments on commit d4b84bc

Please sign in to comment.