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

Use fmt::formatter specialization for std::reference_wrapper to avoid undefined behavior #4164

Merged
merged 1 commit into from
Sep 19, 2024

Conversation

phprus
Copy link
Contributor

@phprus phprus commented Sep 18, 2024

Fix UB: #4163 (comment)

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

enable_if_t<is_formattable<remove_cvref_t<T>, Char>::value>>
: formatter<remove_cvref_t<T>, Char> {
template <typename FormatContext>
auto format(const std::reference_wrapper<T>& ref, FormatContext& ctx) const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pass std::reference_wrapper by value since it should be cheap to copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

test/std-test.cc Outdated Show resolved Hide resolved
… undefined behavior

Signed-off-by: Vladislav Shchapov <[email protected]>
@phprus phprus force-pushed the std-reference_wrapper-ub-1 branch from 0d8da58 to 5f37d3b Compare September 19, 2024 17:03
@phprus phprus requested a review from vitaut September 19, 2024 17:18
@vitaut vitaut merged commit 1f87b1c into fmtlib:master Sep 19, 2024
45 checks passed
@vitaut
Copy link
Contributor

vitaut commented Sep 19, 2024

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants