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

Formatting string-like custom types with an equals sign immediately following closing brace throws an exception #750

Closed
mwinterb opened this issue May 24, 2018 · 1 comment

Comments

@mwinterb
Copy link
Contributor

Assuming this type and formatter:

class StringAnswer {};
namespace fmt
{
template<>
struct formatter<StringAnswer> : formatter<string_view> {
  auto format(StringAnswer, fmt::format_context &ctx) -> decltype(ctx.out()) {
    string_view fourtwo("42");
    return formatter<string_view>::format(fourtwo, ctx);
  }
};
}

And a format call of fmt::format("{}=", StringAnswer());.
In parse_format_specs, it points to }=. For the first iteration of "Parse fill and alignment", p will point to =. The = is treated as an ALIGN_NUMERIC alignment specifier, and the specs_checker handler will throw an exception when on_align is called because the type is not a numeric type.

Adding this check

if (*it == '}')
  return it;

to the very beginning of parse_format_specs fixes this issue and all existing tests pass. It seems like it doesn't (mostly) change the behavior in other scenarios, but I'm puzzled by this test, because I don't see how c could have a value of } except in cases like this. And since my proposed change just seems like such an obvious check since it seems like it would have a high rate of being true most of the time, I feel like I must be missing something. Otherwise this would have been a PR and not an issue.

@vitaut
Copy link
Contributor

vitaut commented May 26, 2018

Good catch, the on_align shouldn't be called here. Moving it after the check in question will solve the problem, but you are right, the check probably belongs at the beginning of the function. Fixed in e2cd521.

@vitaut vitaut closed this as completed May 26, 2018
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

2 participants