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

Incorrect output and segfault with std::optional<std::string> and FMT_COMPILE #3795

Closed
TomCrypto opened this issue Jan 10, 2024 · 2 comments
Closed

Comments

@TomCrypto
Copy link

We've run into an issue that we've tracked down to formatting std::optional<std::string> with {fmt} in conjunction with the FMT_COMPILE macro. We have observed both incorrect formatted strings and also segmentation faults. I've created a minimal repro case on godbolt with clang trunk and {fmt} trunk:

https://godbolt.org/z/rE5xadqf1

The issue does not seem to occur if FMT_COMPILE is not used or if the literal passed to it is just "{}", but as soon as the literal contains more than just that, issues crop up when formatting string optionals. I haven't checked if it happens with other std types. It doesn't appear to occur when formatting plain strings, or std::optional<int>, etc...

The documentation suggests this should be a valid use of FMT_COMPILE so I'm submitting this as a bug.

@phprus
Copy link
Contributor

phprus commented Jan 10, 2024

Simple test case:
https://godbolt.org/z/qe5zK9Phx

@phprus
Copy link
Contributor

phprus commented Jan 10, 2024

@vitaut

This error has been present since the commit: a34a97c

New test:

TEST(compile_test, format_escape) {
  EXPECT_EQ(fmt::format(FMT_COMPILE("{:?}"), "string"), "\"string\"");
  EXPECT_EQ(fmt::format(FMT_COMPILE("0{:?}"), "string"), "0\"string\"");
  EXPECT_EQ(fmt::format(FMT_COMPILE("{:?}0"), "string"), "\"string\"0");
}

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

No branches or pull requests

3 participants