Skip to content

[libc++][format] Don't treat a closing '}' as part of format-spec #81305

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

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libcxx/include/__format/parser_std_format_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class _LIBCPP_TEMPLATE_VIS __parser {
_LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator __parse(_ParseContext& __ctx, __fields __fields) {
auto __begin = __ctx.begin();
auto __end = __ctx.end();
if (__begin == __end)
if (__begin == __end || *__begin == _CharT('}'))
return __begin;

if (__parse_fill_align(__begin, __end, __fields.__use_range_fill_) && __begin == __end)
Expand Down Expand Up @@ -577,9 +577,9 @@ class _LIBCPP_TEMPLATE_VIS __parser {
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_fill_character(_CharT __fill, bool __use_range_fill) {
// The forbidden fill characters all code points formed from a single code unit, thus the
// check can be omitted when more code units are used.
if (__use_range_fill && (__fill == _CharT('{') || __fill == _CharT('}') || __fill == _CharT(':')))
if (__use_range_fill && (__fill == _CharT('{') || __fill == _CharT(':')))
std::__throw_format_error("The fill option contains an invalid value");
else if (__fill == _CharT('{') || __fill == _CharT('}'))
else if (__fill == _CharT('{'))
std::__throw_format_error("The fill option contains an invalid value");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ template <class CharT, class TestFunction, class ExceptionTest>
void test_char_default(TestFunction check, ExceptionTest check_exception, auto&& input) {
// Note when no range-underlying-spec is present the char is escaped,
check(SV("['H', 'e', 'l', 'l', 'o']"), SV("{}"), input);
check(SV("['H', 'e', 'l', 'l', 'o']^42"), SV("{}^42"), input);
check(SV("['H', 'e', 'l', 'l', 'o']^42"), SV("{:}^42"), input);

// when one is present there is no escaping,
check(SV("[H, e, l, l, o]"), SV("{::}"), input);
Expand All @@ -49,7 +51,7 @@ void test_char_default(TestFunction check, ExceptionTest check_exception, auto&&
check(SV("__['H', 'e', 'l', 'l', 'o']___"), SV("{:_^{}}"), input, 30);
check(SV("#####['H', 'e', 'l', 'l', 'o']"), SV("{:#>{}}"), input, 30);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -89,7 +91,7 @@ void test_char_default(TestFunction check, ExceptionTest check_exception, auto&&
check(SV("[_H__, _e__, _l__, _l__, _o__]"), SV("{::_^{}}"), input, 4);
check(SV("[:::H, :::e, :::l, :::l, :::o]"), SV("{:::>{}}"), input, 4);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down Expand Up @@ -149,7 +151,7 @@ void test_char_string(TestFunction check, ExceptionTest check_exception, auto&&
check(SV("_Hello__"), SV("{:_^{}s}"), input, 8);
check(SV("###Hello"), SV("{:#>{}s}"), input, 8);

check_exception("The fill option contains an invalid value", SV("{:}<s}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<s}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<s}"), input);
check_exception("The fill option contains an invalid value", SV("{::<s}"), input);

Expand Down Expand Up @@ -202,7 +204,7 @@ void test_char_escaped_string(TestFunction check, ExceptionTest check_exception,
check(SV(R"(_"Hello"__)"), SV("{:_^{}?s}"), input, 10);
check(SV(R"(###"Hello")"), SV("{:#>{}?s}"), input, 10);

check_exception("The fill option contains an invalid value", SV("{:}<?s}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<?s}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<?s}"), input);
check_exception("The fill option contains an invalid value", SV("{::<?s}"), input);

Expand Down Expand Up @@ -304,6 +306,8 @@ void test_char_to_wchar(TestFunction check, ExceptionTest check_exception) {
template <class CharT, class TestFunction, class ExceptionTest>
void test_bool(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[true, true, false]"), SV("{}"), input);
check(SV("[true, true, false]^42"), SV("{}^42"), input);
check(SV("[true, true, false]^42"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -318,7 +322,7 @@ void test_bool(TestFunction check, ExceptionTest check_exception, auto&& input)
check(SV("__[true, true, false]___"), SV("{:_^{}}"), input, 24);
check(SV("#####[true, true, false]"), SV("{:#>{}}"), input, 24);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -360,7 +364,7 @@ void test_bool(TestFunction check, ExceptionTest check_exception, auto&& input)
check(SV("[_true__, _true__, _false_]"), SV("{::_^{}}"), input, 7);
check(SV("[:::true, :::true, ::false]"), SV("{:::>{}}"), input, 7);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down Expand Up @@ -418,6 +422,8 @@ void test_bool(TestFunction check, ExceptionTest check_exception) {
template <class CharT, class TestFunction, class ExceptionTest>
void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[-42, 1, 2, 42]"), SV("{}"), input);
check(SV("[-42, 1, 2, 42]^42"), SV("{}^42"), input);
check(SV("[-42, 1, 2, 42]^42"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -432,7 +438,7 @@ void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("__[-42, 1, 2, 42]___"), SV("{:_^{}}"), input, 20);
check(SV("#####[-42, 1, 2, 42]"), SV("{:#>{}}"), input, 20);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -474,7 +480,7 @@ void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[_-42_, __1__, __2__, _42__]"), SV("{::_^{}}"), input, 5);
check(SV("[::-42, ::::1, ::::2, :::42]"), SV("{:::>{}}"), input, 5);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down Expand Up @@ -526,6 +532,8 @@ void test_int(TestFunction check, ExceptionTest check_exception) {
template <class CharT, class TestFunction, class ExceptionTest>
void test_floating_point(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[-42.5, 0, 1.25, 42.5]"), SV("{}"), input);
check(SV("[-42.5, 0, 1.25, 42.5]^42"), SV("{}^42"), input);
check(SV("[-42.5, 0, 1.25, 42.5]^42"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -540,7 +548,7 @@ void test_floating_point(TestFunction check, ExceptionTest check_exception, auto
check(SV("__[-42.5, 0, 1.25, 42.5]___"), SV("{:_^{}}"), input, 27);
check(SV("#####[-42.5, 0, 1.25, 42.5]"), SV("{:#>{}}"), input, 27);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -582,7 +590,7 @@ void test_floating_point(TestFunction check, ExceptionTest check_exception, auto
check(SV("[-42.5, __0__, 1.25_, 42.5_]"), SV("{::_^{}}"), input, 5);
check(SV("[-42.5, ::::0, :1.25, :42.5]"), SV("{:::>{}}"), input, 5);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down Expand Up @@ -662,6 +670,8 @@ void test_floating_point(TestFunction check, ExceptionTest check_exception) {
template <class CharT, class TestFunction, class ExceptionTest>
void test_pointer(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[0x0]"), SV("{}"), input);
check(SV("[0x0]^42"), SV("{}^42"), input);
check(SV("[0x0]^42"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -676,7 +686,7 @@ void test_pointer(TestFunction check, ExceptionTest check_exception, auto&& inpu
check(SV("__[0x0]___"), SV("{:_^{}}"), input, 10);
check(SV("#####[0x0]"), SV("{:#>{}}"), input, 10);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -716,7 +726,7 @@ void test_pointer(TestFunction check, ExceptionTest check_exception, auto&& inpu
check(SV("[_0x0_]"), SV("{::_^{}}"), input, 5);
check(SV("[::0x0]"), SV("{:::>{}}"), input, 5);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down Expand Up @@ -770,6 +780,8 @@ void test_pointer(TestFunction check, ExceptionTest check_exception) {
template <class CharT, class TestFunction, class ExceptionTest>
void test_string(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV(R"(["Hello", "world"])"), SV("{}"), input);
check(SV(R"(["Hello", "world"]^42)"), SV("{}^42"), input);
check(SV(R"(["Hello", "world"]^42)"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -784,7 +796,7 @@ void test_string(TestFunction check, ExceptionTest check_exception, auto&& input
check(SV(R"(__["Hello", "world"]___)"), SV("{:_^{}}"), input, 23);
check(SV(R"(#####["Hello", "world"])"), SV("{:#>{}}"), input, 23);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -824,7 +836,7 @@ void test_string(TestFunction check, ExceptionTest check_exception, auto&& input
check(SV(R"([_Hello__, _world__])"), SV("{::_^{}}"), input, 8);
check(SV(R"([:::Hello, :::world])"), SV("{:::>{}}"), input, 8);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down Expand Up @@ -880,6 +892,8 @@ void test_string(TestFunction check, ExceptionTest check_exception) {
template <class CharT, class TestFunction, class ExceptionTest>
void test_status(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[0xaaaa, 0x5555, 0xaa55]"), SV("{}"), input);
check(SV("[0xaaaa, 0x5555, 0xaa55]^42"), SV("{}^42"), input);
check(SV("[0xaaaa, 0x5555, 0xaa55]^42"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -894,7 +908,7 @@ void test_status(TestFunction check, ExceptionTest check_exception, auto&& input
check(SV("__[0xaaaa, 0x5555, 0xaa55]___"), SV("{:_^{}}"), input, 29);
check(SV("#####[0xaaaa, 0x5555, 0xaa55]"), SV("{:#>{}}"), input, 29);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
template <class CharT, class TestFunction, class ExceptionTest>
void format_test_vector_bool(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("[true, true, false]"), SV("{}"), input);
check(SV("[true, true, false]^42"), SV("{}^42"), input);
check(SV("[true, true, false]^42"), SV("{:}^42"), input);

// ***** underlying has no format-spec

Expand All @@ -30,7 +32,7 @@ void format_test_vector_bool(TestFunction check, ExceptionTest check_exception,
check(SV("__[true, true, false]___"), SV("{:_^{}}"), input, 24);
check(SV("#####[true, true, false]"), SV("{:#>{}}"), input, 24);

check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

Expand Down Expand Up @@ -73,7 +75,7 @@ void format_test_vector_bool(TestFunction check, ExceptionTest check_exception,
check(SV("[_true__, _true__, _false_]"), SV("{::_^{}}"), input, 7);
check(SV("[:::true, :::true, ::false]"), SV("{:::>{}}"), input, 7);

check_exception("The fill option contains an invalid value", SV("{::}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

// *** sign ***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
/***** Test the type specific part *****/
#if !defined(__APPLE__) && !defined(__FreeBSD__)
check(SV("0"), SV("{}"), input);
check(SV("0^42"), SV("{}^42"), input);
check(SV("0^42"), SV("{:}^42"), input);

// *** align-fill & width ***
check(SV(" 0"), SV("{:5}"), input);
Expand All @@ -36,6 +38,8 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
check(SV("####0"), SV("{:#>{}}"), input, 5);
#else // !defined(__APPLE__) && !defined(__FreeBSD__)
check(SV("0x0"), SV("{}"), input);
check(SV("0x0^42"), SV("{}^42"), input);
check(SV("0x0^42"), SV("{:}^42"), input);

// *** align-fill & width ***
check(SV(" 0x0"), SV("{:7}"), input);
Expand All @@ -50,7 +54,7 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
#endif // !defined(__APPLE__) && !defined(__FreeBSD__)

/***** Test the type generic part *****/
check_exception("The fill option contains an invalid value", SV("{:}<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);

// *** sign ***
Expand Down
Loading