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

Measure display width in tuple formatter #4631

Merged
merged 1 commit into from
May 20, 2024

Conversation

cpplearner
Copy link
Contributor

@cpplearner cpplearner commented Apr 26, 2024

Currently, the tuple formatter uses static_cast<int>(_Tmp_buf.size()) to compute the width of output (in order to determine the number of fill characters to insert). This gives the wrong width if

  1. The output contains multibyte Unicode characters, or
  2. _Tmp_buf.size() is larger than INT_MAX (see <format>: Misbehavior for huge strings #4479).

Both problems can be avoided by using _Measure_string_prefix, which computes the display width and clamps the result to INT_MAX (which means no fill character will be inserted).

Closes #4479

While working on this, I noticed that the tuple formatter test uses \u00d6 which cannot be represented in some legacy text encodings. This PR adjusted the test to make it encoding-independent.

Closes #4635

Future work: if no width is specified in the format string, there should be no need to use a temporary buffer. The use of _Tmp_buf should be skipped in this case.

@cpplearner cpplearner requested a review from a team as a code owner April 26, 2024 13:33
@StephanTLavavej StephanTLavavej added bug Something isn't working format C++20/23 format labels Apr 26, 2024
@StephanTLavavej StephanTLavavej self-assigned this Apr 26, 2024
fsb4000

This comment was marked as resolved.

stl/inc/format Show resolved Hide resolved
@StephanTLavavej
Copy link
Member

I'm speculatively mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit b872c42 into microsoft:main May 20, 2024
39 checks passed
@StephanTLavavej
Copy link
Member

Thanks for fixing this runtime correctness bug in a hopefully-popular new feature! 😻 🐞 🛠️

JMazurkiewicz added a commit to JMazurkiewicz/STL that referenced this pull request May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working format C++20/23 format
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

P2286R8_text_formatting_tuple fails on non-English locale <format>: Misbehavior for huge strings
4 participants