[clang] 8322a30 - Avoid accessing unset optional, workaround for #100095 (#100408)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 26 05:25:00 PDT 2024


Author: Alexander Kornienko
Date: 2024-07-26T14:24:56+02:00
New Revision: 8322a3085c902de9759a40ccdcd194a4c46e7185

URL: https://github.com/llvm/llvm-project/commit/8322a3085c902de9759a40ccdcd194a4c46e7185
DIFF: https://github.com/llvm/llvm-project/commit/8322a3085c902de9759a40ccdcd194a4c46e7185.diff

LOG: Avoid accessing unset optional, workaround for #100095 (#100408)

This patch avoids accessing an unset `std::optional<>`, which is a part
of the manifestation of #100095. The other part is an assertion failure
that is not addressed here. This is not a proper fix, but enables Clang
to continue working with more libc++ runtime checks enabled
(specifically, `-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST`,
which checks access to unset optionals among other things). A proper fix
is being discussed on #100095.

Added: 
    clang/test/SemaCXX/pr100095.cpp

Modified: 
    clang/lib/Sema/SemaTemplateDeduction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index b7b857ebf804b..db7f233dcef73 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -951,9 +951,11 @@ class PackDeductionScope {
 
     // Skip over the pack elements that were expanded into separate arguments.
     // If we partially expanded, this is the number of partial arguments.
+    // FIXME: `&& FixedNumExpansions` is a workaround for UB described in
+    // https://github.com/llvm/llvm-project/issues/100095
     if (IsPartiallyExpanded)
       PackElements += NumPartialPackArgs;
-    else if (IsExpanded)
+    else if (IsExpanded && FixedNumExpansions)
       PackElements += *FixedNumExpansions;
 
     for (auto &Pack : Packs) {

diff  --git a/clang/test/SemaCXX/pr100095.cpp b/clang/test/SemaCXX/pr100095.cpp
new file mode 100644
index 0000000000000..15913fec9d5ae
--- /dev/null
+++ b/clang/test/SemaCXX/pr100095.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s
+// XFAIL: asserts
+
+template <class> struct Pair;
+template <class...> struct Tuple {
+  template <class _Up> Tuple(_Up);
+};
+template <typename> struct StatusOr;
+template <int> using ElementType = int;
+template <int... fields>
+using Key = Tuple<ElementType<fields>...>;
+template <int... fields>
+StatusOr<Pair<Key<fields...>>> Parser();
+struct Helper { Helper(Tuple<>, Tuple<>, int, int); };
+struct D : Helper {
+  D(Key<> f, int n, int e) : Helper(f, Parser<>, n, e) {}
+};


        


More information about the cfe-commits mailing list