Skip to content

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

Merged
vitaut merged 1 commit intofmtlib:masterfrom
phprus:std-reference_wrapper-ub-1
Sep 19, 2024
Merged

Use fmt::formatter specialization for std::reference_wrapper to avoid undefined behavior#4164
vitaut merged 1 commit intofmtlib:masterfrom
phprus:std-reference_wrapper-ub-1

Conversation

@phprus
Copy link
Copy Markdown
Contributor

@phprus phprus commented Sep 18, 2024

Fix UB: #4163 (comment)

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

… undefined behavior

Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
@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
@vitaut
Copy link
Copy Markdown
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