[libcxx-commits] [PATCH] D143402: [libc++][format] Fixes constexpr validation.

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 6 07:31:49 PST 2023


Mordante created this revision.
Herald added a project: All.
Mordante requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

The constexpr validation parsed parts of the format string that didn't
belong to the specific replacement field.

Fixes https://llvm.org/PR60536


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143402

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


Index: libcxx/test/std/utilities/format/format.functions/format_tests.h
===================================================================
--- libcxx/test/std/utilities/format/format.functions/format_tests.h
+++ libcxx/test/std/utilities/format/format.functions/format_tests.h
@@ -2617,6 +2617,10 @@
 
   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);
Index: libcxx/include/__format/format_functions.h
===================================================================
--- libcxx/include/__format/format_functions.h
+++ libcxx/include/__format/format_functions.h
@@ -261,10 +261,12 @@
 
   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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143402.495131.patch
Type: text/x-patch
Size: 1511 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230206/d437d1ef/attachment.bin>


More information about the libcxx-commits mailing list