Skip to content

Commit

Permalink
[libc++][format] Fixes constexpr validation.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mordante authored and tstellar committed Feb 8, 2023
1 parent c41085e commit 22000e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions libcxx/include/__format/format_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,12 @@ __handle_replacement_field(const _CharT* __begin, const _CharT* __end,

if constexpr (same_as<_Ctx, __compile_time_basic_format_context<_CharT>>) {
__arg_t __type = __ctx.arg(__r.__value);
if (__type == __arg_t::__handle)
if (__type == __arg_t::__none)
std::__throw_format_error("Argument index out of bounds");
else if (__type == __arg_t::__handle)
__ctx.__handle(__r.__value).__parse(__parse_ctx);
else
__format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
else if (__parse)
__format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
} else
_VSTD::__visit_format_arg(
[&](auto __arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,10 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {

check(SV("{"), SV("{{"));
check(SV("}"), SV("}}"));
check(SV("{:^}"), SV("{{:^}}"));
check(SV("{: ^}"), SV("{{:{}^}}"), CharT(' '));
check(SV("{:{}^}"), SV("{{:{{}}^}}"));
check(SV("{:{ }^}"), SV("{{:{{{}}}^}}"), CharT(' '));

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

0 comments on commit 22000e3

Please sign in to comment.