Skip to content

Commit 22000e3

Browse files
mordantetstellar
authored andcommitted
[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 (cherry picked from commit ac44dad)
1 parent c41085e commit 22000e3

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
@@ -258,10 +258,12 @@ __handle_replacement_field(const _CharT* __begin, const _CharT* __end,
258258

259259
if constexpr (same_as<_Ctx, __compile_time_basic_format_context<_CharT>>) {
260260
__arg_t __type = __ctx.arg(__r.__value);
261-
if (__type == __arg_t::__handle)
261+
if (__type == __arg_t::__none)
262+
std::__throw_format_error("Argument index out of bounds");
263+
else if (__type == __arg_t::__handle)
262264
__ctx.__handle(__r.__value).__parse(__parse_ctx);
263-
else
264-
__format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
265+
else if (__parse)
266+
__format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
265267
} else
266268
_VSTD::__visit_format_arg(
267269
[&](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)