Skip to content

Commit ac44dad

Browse files
committed
[libc++][format] Fixes constexpr validation.
The constexpr validation parsed parts of the format string that didn't belong to the specific replacement field. Fixes https://llvm.org/PR60536 Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D143402
1 parent 0fc42b3 commit ac44dad

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

libcxx/include/__format/format_functions.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,12 @@ __handle_replacement_field(_Iterator __begin, _Iterator __end,
261261

262262
if constexpr (same_as<_Ctx, __compile_time_basic_format_context<_CharT>>) {
263263
__arg_t __type = __ctx.arg(__r.__value);
264-
if (__type == __arg_t::__handle)
264+
if (__type == __arg_t::__none)
265+
std::__throw_format_error("Argument index out of bounds");
266+
else if (__type == __arg_t::__handle)
265267
__ctx.__handle(__r.__value).__parse(__parse_ctx);
266-
else
267-
__format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
268+
else if (__parse)
269+
__format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
268270
} else
269271
_VSTD::__visit_format_arg(
270272
[&](auto __arg) {

libcxx/test/std/utilities/format/format.functions/format_tests.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,10 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
26172617

26182618
check(SV("{"), SV("{{"));
26192619
check(SV("}"), SV("}}"));
2620+
check(SV("{:^}"), SV("{{:^}}"));
2621+
check(SV("{: ^}"), SV("{{:{}^}}"), CharT(' '));
2622+
check(SV("{:{}^}"), SV("{{:{{}}^}}"));
2623+
check(SV("{:{ }^}"), SV("{{:{{{}}}^}}"), CharT(' '));
26202624

26212625
// *** Test argument ID ***
26222626
check(SV("hello false true"), SV("hello {0:} {1:}"), false, true);

0 commit comments

Comments
 (0)