Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<format>: crash when formatting a floating-point value with #/L and a large precision #4907

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 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,23 @@ 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
void test_gh_4320() {
Andor233 marked this conversation as resolved.
Show resolved Hide resolved
assert(format("{:#.1075e}", 1.0)
== "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 +1621,7 @@ void test() {

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

int main() {
Expand Down