[llvm-branch-commits] [libcxx] 22000e3 - [libc++][format] Fixes constexpr validation.

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Feb 7 18:47:10 PST 2023


Author: Mark de Wever
Date: 2023-02-07T18:46:12-08:00
New Revision: 22000e3c8d1aa4e1d9530624ecc25f3096434af3

URL: https://github.com/llvm/llvm-project/commit/22000e3c8d1aa4e1d9530624ecc25f3096434af3
DIFF: https://github.com/llvm/llvm-project/commit/22000e3c8d1aa4e1d9530624ecc25f3096434af3.diff

LOG: [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 ac44dadcbea52e14aab20c0d590d4ec00d615a93)

Added: 
    

Modified: 
    libcxx/include/__format/format_functions.h
    libcxx/test/std/utilities/format/format.functions/format_tests.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/format_functions.h b/libcxx/include/__format/format_functions.h
index 185148ccba53..0f0001272d40 100644
--- a/libcxx/include/__format/format_functions.h
+++ b/libcxx/include/__format/format_functions.h
@@ -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) {

diff  --git a/libcxx/test/std/utilities/format/format.functions/format_tests.h b/libcxx/test/std/utilities/format/format.functions/format_tests.h
index 86d11f2c26be..763cf4517bff 100644
--- a/libcxx/test/std/utilities/format/format.functions/format_tests.h
+++ b/libcxx/test/std/utilities/format/format.functions/format_tests.h
@@ -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);


        


More information about the llvm-branch-commits mailing list