Skip to content

Commit d4b84bc

Browse files
authored
<format>: crash when formatting a floating-point value with #/L and a large precision (#4907)
1 parent 138e116 commit d4b84bc

File tree

2 files changed

+23
-1
lines changed
  • stl/inc
  • tests/std/tests/P0645R10_text_formatting_formatting

2 files changed

+23
-1
lines changed

stl/inc/format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ _NODISCARD _OutputIt _Fmt_write(
30953095
switch (_Format) {
30963096
case chars_format::hex:
30973097
case chars_format::scientific:
3098-
if (_Extra_precision != 0) {
3098+
if (_Extra_precision != 0 && !_Specs._Alt && !_Specs._Localized) {
30993099
// Trailing zeroes are in front of the exponent
31003100
while (*--_Exponent_start != _Exponent) {
31013101
}

tests/std/tests/P0645R10_text_formatting_formatting/test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,26 @@ void test_gh_4319() {
15201520
assert(format(STR("{:}"), 12345678.0) == STR("12345678"));
15211521
}
15221522

1523+
// GH-4320: <format>: crash when formatting a floating-point value with #/L and a large precision
1524+
template <class charT>
1525+
void test_gh_4320() {
1526+
assert(
1527+
format(STR("{:#.1075e}"), 1.0)
1528+
== STR(
1529+
"1."
1530+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1531+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1532+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1533+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1534+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1535+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1536+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1537+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1538+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1539+
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1540+
"00000000000000000000000000000000000e+00"));
1541+
}
1542+
15231543
void test() {
15241544
test_simple_formatting<char>();
15251545
test_simple_formatting<wchar_t>();
@@ -1604,6 +1624,8 @@ void test() {
16041624

16051625
test_gh_4319<char>();
16061626
test_gh_4319<wchar_t>();
1627+
test_gh_4320<char>();
1628+
test_gh_4320<wchar_t>();
16071629
}
16081630

16091631
int main() {

0 commit comments

Comments
 (0)