From 2b3d3ec2e7d0b914f84c2f02286c6364eded7b6d Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:03:49 +0800 Subject: [PATCH 1/2] Fix #4320: : crash when formatting a floating-point value with #/L and a large precision --- stl/inc/format | 2 +- .../test.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/stl/inc/format b/stl/inc/format index c2af86f05a..e5ac72dcc0 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -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) { } diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index c6295a44c3..b142d3c2e6 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -1520,6 +1520,23 @@ void test_gh_4319() { assert(format(STR("{:}"), 12345678.0) == STR("12345678")); } +// GH-4320: : crash when formatting a floating-point value with #/L and a large precision +void test_gh_4320() { + assert(format("{:#.1075e}", 1.0) + == "1." + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000e+00"); +} + void test() { test_simple_formatting(); test_simple_formatting(); @@ -1604,6 +1621,7 @@ void test() { test_gh_4319(); test_gh_4319(); + test_gh_4320(); } int main() { From 70bb7fdb6b8095e0717036e0a0c3116a545ea485 Mon Sep 17 00:00:00 2001 From: andor <16273755+Andor233@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:47:15 +0800 Subject: [PATCH 2/2] Add wchar_t test for gh_4320 --- .../test.cpp | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index b142d3c2e6..0b0b569f1c 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -1521,20 +1521,23 @@ void test_gh_4319() { } // GH-4320: : crash when formatting a floating-point value with #/L and a large precision +template void test_gh_4320() { - assert(format("{:#.1075e}", 1.0) - == "1." - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "00000000000000000000000000000000000e+00"); + assert( + format(STR("{:#.1075e}"), 1.0) + == STR( + "1." + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000e+00")); } void test() { @@ -1621,7 +1624,8 @@ void test() { test_gh_4319(); test_gh_4319(); - test_gh_4320(); + test_gh_4320(); + test_gh_4320(); } int main() {