[clang] [libcxx] [clang][Sema] Add checks for validity of default ctor's class (PR #78898)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 9 07:22:12 PST 2024


https://github.com/Endilll updated https://github.com/llvm/llvm-project/pull/78898

>From b99a75a8756a7841657fc78ffbd40f780a412f2b Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Sun, 21 Jan 2024 16:26:29 +0300
Subject: [PATCH 1/3] [clang][Sema] Add checks for validity of default ctor's
 class

Fixes #10518
Fixes #67914
Fixes #78388
Also addresses the second example in #49103

This patch is based on suggestion from @cor3ntin in https://github.com/llvm/llvm-project/issues/67914#issuecomment-1896011898
---
 clang/docs/ReleaseNotes.rst    | 5 +++++
 clang/lib/Sema/SemaDeclCXX.cpp | 7 +++++++
 2 files changed, 12 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8bb26fcae18d6b..5971bda21a5e25 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1013,6 +1013,11 @@ Bug Fixes to C++ Support
 - Fix a false-positive ODR violation for different definitions for `std::align_val_t`.
   Fixes (`#76638 <https://github.com/llvm/llvm-project/issues/76638>`_)
 
+- Fix crash when calling the constructor of an invalid class.
+  Fixes (`#10518 <https://github.com/llvm/llvm-project/issues/10518>`_),
+  (`#67914 <https://github.com/llvm/llvm-project/issues/10518>`_),
+  and (`#78388 <https://github.com/llvm/llvm-project/issues/78388>`_)
+
 - Remove recorded `#pragma once` state for headers included in named modules.
   Fixes (`#77995 <https://github.com/llvm/llvm-project/issues/77995>`_)
 
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index df5bd55e7c2836..634af573480b45 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -5990,6 +5990,10 @@ void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) {
 
   if (CXXConstructorDecl *Constructor
       = dyn_cast<CXXConstructorDecl>(CDtorDecl)) {
+    if (CXXRecordDecl *ClassDecl = Constructor->getParent();
+        !ClassDecl || ClassDecl->isInvalidDecl()) {
+      return;
+    }
     SetCtorInitializers(Constructor, /*AnyErrors=*/false);
     DiagnoseUninitializedFields(*this, Constructor);
   }
@@ -14030,6 +14034,9 @@ void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation,
 
   CXXRecordDecl *ClassDecl = Constructor->getParent();
   assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor");
+  if (ClassDecl->isInvalidDecl()) {
+    return;
+  }
 
   SynthesizedFunctionScope Scope(*this, Constructor);
 

>From 826dc7aa7cad2c2d2eaa02b30c814e63abf222b6 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Fri, 26 Jan 2024 08:50:52 +0300
Subject: [PATCH 2/3] Add regression tests for fixed crashes

---
 clang/test/SemaCXX/crash-GH10518.cpp   | 22 ++++++++
 clang/test/SemaCXX/crash-GH49103-2.cpp | 13 +++++
 clang/test/SemaCXX/crash-GH67914.cpp   | 78 ++++++++++++++++++++++++++
 clang/test/SemaCXX/crash-GH78388.cpp   | 17 ++++++
 4 files changed, 130 insertions(+)
 create mode 100644 clang/test/SemaCXX/crash-GH10518.cpp
 create mode 100644 clang/test/SemaCXX/crash-GH49103-2.cpp
 create mode 100644 clang/test/SemaCXX/crash-GH67914.cpp
 create mode 100644 clang/test/SemaCXX/crash-GH78388.cpp

diff --git a/clang/test/SemaCXX/crash-GH10518.cpp b/clang/test/SemaCXX/crash-GH10518.cpp
new file mode 100644
index 00000000000000..6c5f80afd3cf8b
--- /dev/null
+++ b/clang/test/SemaCXX/crash-GH10518.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -verify -std=c++98 %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
+// RUN: %clang_cc1 -verify -std=c++14 %s
+// RUN: %clang_cc1 -verify -std=c++17 %s
+// RUN: %clang_cc1 -verify -std=c++20 %s
+// RUN: %clang_cc1 -verify -std=c++23 %s
+// RUN: %clang_cc1 -verify -std=c++2c %s
+
+// https://github.com/llvm/llvm-project/issues/10518
+
+template <class T>
+class A : public T {
+};
+
+template <class T>
+class B : public A<T> {
+};
+
+template <class T>
+class B<int> : public A<T> { // expected-error 0-1 {{}}
+	B(T *t) {}
+};
diff --git a/clang/test/SemaCXX/crash-GH49103-2.cpp b/clang/test/SemaCXX/crash-GH49103-2.cpp
new file mode 100644
index 00000000000000..4c17a054c73afc
--- /dev/null
+++ b/clang/test/SemaCXX/crash-GH49103-2.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -verify -std=c++98 %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
+// RUN: %clang_cc1 -verify -std=c++14 %s
+// RUN: %clang_cc1 -verify -std=c++17 %s
+// RUN: %clang_cc1 -verify -std=c++20 %s
+// RUN: %clang_cc1 -verify -std=c++23 %s
+// RUN: %clang_cc1 -verify -std=c++2c %s
+
+// https://github.com/llvm/llvm-project/issues/49103
+
+template<class> struct A; // expected-note 0+ {{}}
+struct S : __make_integer_seq<A, int, 42> { }; // expected-error 0+ {{}}
+S s;
diff --git a/clang/test/SemaCXX/crash-GH67914.cpp b/clang/test/SemaCXX/crash-GH67914.cpp
new file mode 100644
index 00000000000000..fbaeac636c0db1
--- /dev/null
+++ b/clang/test/SemaCXX/crash-GH67914.cpp
@@ -0,0 +1,78 @@
+// RUN: %clang_cc1 -verify -std=c++98 %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
+// RUN: %clang_cc1 -verify -std=c++14 %s
+// RUN: %clang_cc1 -verify -std=c++17 %s
+// RUN: %clang_cc1 -verify -std=c++20 %s
+// RUN: %clang_cc1 -verify -std=c++23 %s
+// RUN: %clang_cc1 -verify -std=c++2c %s
+
+// https://github.com/llvm/llvm-project/issues/67914
+
+template < typename, int >
+struct Mask;
+
+template < int, class >
+struct conditional {
+  using type = Mask< int, 16 >; // expected-warning 0+ {{}}
+};
+
+template < class _Then >
+struct conditional< 0, _Then > {
+  using type = _Then; // expected-warning 0+ {{}}
+};
+
+template < int _Bp, class, class _Then >
+using conditional_t = typename conditional< _Bp, _Then >::type; // expected-warning 0+ {{}}
+
+template < typename, int >
+struct Array;
+
+template < typename, int, bool, typename >
+struct StaticArrayImpl;
+
+template < typename Value_, int Size_ >
+struct Mask : StaticArrayImpl< Value_, Size_, 1, Mask< Value_, Size_ > > { // expected-note 0+ {{}}
+  template < typename T1 >
+  Mask(T1) {} // expected-note 0+ {{}}
+};
+
+template < typename T >
+void load(typename T::MaskType mask) {
+  T::load_(mask); // expected-note 0+ {{}}
+}
+
+template < typename Value_, int IsMask_, typename Derived_ >
+struct StaticArrayImpl< Value_, 32, IsMask_, Derived_ > {
+  using Array1 = conditional_t< IsMask_, void, Array< Value_, 16 > >; // expected-warning 0+ {{}}
+  
+  template < typename Mask >
+  static Derived_ load_(Mask mask) {
+    return Derived_{load< Array1 >(mask.a1), Mask{}}; // expected-error 0+ {{}}
+  }
+
+  Array1 a1;
+};
+
+template < typename Derived_ >
+struct KMaskBase;
+
+template < typename Derived_ >
+struct StaticArrayImpl< float, 16, 0, Derived_ > {
+  template < typename Mask >
+  static Derived_ load_(Mask mask);
+};
+
+template < typename Derived_ >
+struct StaticArrayImpl< float, 16, 1, Mask< float, 16 > > : KMaskBase< Derived_ > {}; // expected-error 0+ {{}}
+
+template < typename Derived_ >
+struct StaticArrayImpl< int, 16, 1, Derived_ > {};
+
+template < typename Value_, int Size_ >
+struct Array : StaticArrayImpl< Value_, Size_, 0, Array< Value_, Size_ > > {
+  using MaskType = Mask< Value_, Size_ >; // expected-warning 0+ {{}}
+};
+
+void test11_load_masked() {
+  load< Array< float, 32 > >{} == 0; // expected-error 0+ {{}} expected-warning 0+ {{}} expected-note 0+ {{}}
+}
diff --git a/clang/test/SemaCXX/crash-GH78388.cpp b/clang/test/SemaCXX/crash-GH78388.cpp
new file mode 100644
index 00000000000000..cdec4d5bedef4a
--- /dev/null
+++ b/clang/test/SemaCXX/crash-GH78388.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -verify -std=c++98 %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
+// RUN: %clang_cc1 -verify -std=c++14 %s
+// RUN: %clang_cc1 -verify -std=c++17 %s
+// RUN: %clang_cc1 -verify -std=c++20 %s
+// RUN: %clang_cc1 -verify -std=c++23 %s
+// RUN: %clang_cc1 -verify -std=c++2c %s
+
+// https://github.com/llvm/llvm-project/issues/78388
+
+typedef mbstate_t; // expected-error 0+ {{}} expected-note 0+ {{}}
+  template < typename , typename , typename >
+  class a // expected-error 0+ {{}}
+  class b { // expected-error 0+ {{}}
+    namespace { // expected-note 0+ {{}} expected-note 0+ {{}}
+    template < typename c > b::operator=() { // expected-error 0+ {{}} expected-note 0+ {{}}
+      struct :a< c, char, stdmbstate_t > d // expected-error 0+ {{}} expected-warning 0+ {{}}

>From 60624f72877fcbeac67db97ac78f0b3bdcea7bfd Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Fri, 9 Feb 2024 18:21:57 +0300
Subject: [PATCH 3/3] Silence libc++ transform_error.mandates.verify.cpp test

@philnik said that static asserts are the only interesting part of the test
---
 .../expected/expected.void/transform_error.mandates.verify.cpp  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/utilities/expected/expected.void/transform_error.mandates.verify.cpp b/libcxx/test/libcxx/utilities/expected/expected.void/transform_error.mandates.verify.cpp
index 4f4f5839361e2a..508b01a7bcea39 100644
--- a/libcxx/test/libcxx/utilities/expected/expected.void/transform_error.mandates.verify.cpp
+++ b/libcxx/test/libcxx/utilities/expected/expected.void/transform_error.mandates.verify.cpp
@@ -56,7 +56,7 @@ void test() {
     e.transform_error(return_unexpected<int&>); // expected-error-re@*:* {{static assertion failed {{.*}}The result of {{.*}} must be a valid template argument for unexpected}}
     // expected-error-re@*:* 0-1 {{{{(excess elements in struct initializer|no matching constructor for initialization of)}}{{.*}}}}
     // expected-error-re@*:* {{static assertion failed {{.*}}A program that instantiates expected<T, E> with a E that is not a valid argument for unexpected<E> is ill-formed}}
-    // expected-error-re@*:* {{call to deleted constructor of {{.*}}}}
+    // expected-error-re@*:* 0-1 {{call to deleted constructor of {{.*}}}}
     // expected-error-re@*:* {{union member {{.*}} has reference type {{.*}}}}
 
     e.transform_error(return_no_object<int&>); // expected-error-re@*:* {{static assertion failed {{.*}}The result of {{.*}} must be a valid template argument for unexpected}}



More information about the cfe-commits mailing list