Skip to content

Commit c58c6aa

Browse files
authored
[clang][Sema] Add checks for validity of default ctor's class (#78898)
Fixes #10518 Fixes #67914 Fixes #78388 Also addresses the second example in #49103 This patch is based on suggestion from @cor3ntin in #67914 (comment)
1 parent 94272a5 commit c58c6aa

File tree

7 files changed

+142
-1
lines changed

7 files changed

+142
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ Bug Fixes to Attribute Support
182182
Bug Fixes to C++ Support
183183
^^^^^^^^^^^^^^^^^^^^^^^^
184184

185+
- Fix crash when calling the constructor of an invalid class.
186+
Fixes (`#10518 <https://github.com/llvm/llvm-project/issues/10518>`_),
187+
(`#67914 <https://github.com/llvm/llvm-project/issues/10518>`_),
188+
and (`#78388 <https://github.com/llvm/llvm-project/issues/78388>`_)
185189
- Fix crash when using lifetimebound attribute in function with trailing return.
186190
Fixes (`#73619 <https://github.com/llvm/llvm-project/issues/73619>`_)
187191
- Addressed an issue where constraints involving injected class types are perceived

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5998,6 +5998,10 @@ void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) {
59985998

59995999
if (CXXConstructorDecl *Constructor
60006000
= dyn_cast<CXXConstructorDecl>(CDtorDecl)) {
6001+
if (CXXRecordDecl *ClassDecl = Constructor->getParent();
6002+
!ClassDecl || ClassDecl->isInvalidDecl()) {
6003+
return;
6004+
}
60016005
SetCtorInitializers(Constructor, /*AnyErrors=*/false);
60026006
DiagnoseUninitializedFields(*this, Constructor);
60036007
}
@@ -14038,6 +14042,9 @@ void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation,
1403814042

1403914043
CXXRecordDecl *ClassDecl = Constructor->getParent();
1404014044
assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor");
14045+
if (ClassDecl->isInvalidDecl()) {
14046+
return;
14047+
}
1404114048

1404214049
SynthesizedFunctionScope Scope(*this, Constructor);
1404314050

clang/test/SemaCXX/crash-GH10518.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -verify -std=c++98 %s
2+
// RUN: %clang_cc1 -verify -std=c++11 %s
3+
// RUN: %clang_cc1 -verify -std=c++14 %s
4+
// RUN: %clang_cc1 -verify -std=c++17 %s
5+
// RUN: %clang_cc1 -verify -std=c++20 %s
6+
// RUN: %clang_cc1 -verify -std=c++23 %s
7+
// RUN: %clang_cc1 -verify -std=c++2c %s
8+
9+
// https://github.com/llvm/llvm-project/issues/10518
10+
11+
template <class T>
12+
class A : public T {
13+
};
14+
15+
template <class T>
16+
class B : public A<T> {
17+
};
18+
19+
template <class T>
20+
class B<int> : public A<T> { // expected-error 0-1 {{}}
21+
B(T *t) {}
22+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -verify -std=c++98 %s
2+
// RUN: %clang_cc1 -verify -std=c++11 %s
3+
// RUN: %clang_cc1 -verify -std=c++14 %s
4+
// RUN: %clang_cc1 -verify -std=c++17 %s
5+
// RUN: %clang_cc1 -verify -std=c++20 %s
6+
// RUN: %clang_cc1 -verify -std=c++23 %s
7+
// RUN: %clang_cc1 -verify -std=c++2c %s
8+
9+
// https://github.com/llvm/llvm-project/issues/49103
10+
11+
template<class> struct A; // expected-note 0+ {{}}
12+
struct S : __make_integer_seq<A, int, 42> { }; // expected-error 0+ {{}}
13+
S s;

clang/test/SemaCXX/crash-GH67914.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// RUN: %clang_cc1 -verify -std=c++98 %s
2+
// RUN: %clang_cc1 -verify -std=c++11 %s
3+
// RUN: %clang_cc1 -verify -std=c++14 %s
4+
// RUN: %clang_cc1 -verify -std=c++17 %s
5+
// RUN: %clang_cc1 -verify -std=c++20 %s
6+
// RUN: %clang_cc1 -verify -std=c++23 %s
7+
// RUN: %clang_cc1 -verify -std=c++2c %s
8+
9+
// https://github.com/llvm/llvm-project/issues/67914
10+
11+
template < typename, int >
12+
struct Mask;
13+
14+
template < int, class >
15+
struct conditional {
16+
using type = Mask< int, 16 >; // expected-warning 0+ {{}}
17+
};
18+
19+
template < class _Then >
20+
struct conditional< 0, _Then > {
21+
using type = _Then; // expected-warning 0+ {{}}
22+
};
23+
24+
template < int _Bp, class, class _Then >
25+
using conditional_t = typename conditional< _Bp, _Then >::type; // expected-warning 0+ {{}}
26+
27+
template < typename, int >
28+
struct Array;
29+
30+
template < typename, int, bool, typename >
31+
struct StaticArrayImpl;
32+
33+
template < typename Value_, int Size_ >
34+
struct Mask : StaticArrayImpl< Value_, Size_, 1, Mask< Value_, Size_ > > { // expected-note 0+ {{}}
35+
template < typename T1 >
36+
Mask(T1) {} // expected-note 0+ {{}}
37+
};
38+
39+
template < typename T >
40+
void load(typename T::MaskType mask) {
41+
T::load_(mask); // expected-note 0+ {{}}
42+
}
43+
44+
template < typename Value_, int IsMask_, typename Derived_ >
45+
struct StaticArrayImpl< Value_, 32, IsMask_, Derived_ > {
46+
using Array1 = conditional_t< IsMask_, void, Array< Value_, 16 > >; // expected-warning 0+ {{}}
47+
48+
template < typename Mask >
49+
static Derived_ load_(Mask mask) {
50+
return Derived_{load< Array1 >(mask.a1), Mask{}}; // expected-error 0+ {{}}
51+
}
52+
53+
Array1 a1;
54+
};
55+
56+
template < typename Derived_ >
57+
struct KMaskBase;
58+
59+
template < typename Derived_ >
60+
struct StaticArrayImpl< float, 16, 0, Derived_ > {
61+
template < typename Mask >
62+
static Derived_ load_(Mask mask);
63+
};
64+
65+
template < typename Derived_ >
66+
struct StaticArrayImpl< float, 16, 1, Mask< float, 16 > > : KMaskBase< Derived_ > {}; // expected-error 0+ {{}}
67+
68+
template < typename Derived_ >
69+
struct StaticArrayImpl< int, 16, 1, Derived_ > {};
70+
71+
template < typename Value_, int Size_ >
72+
struct Array : StaticArrayImpl< Value_, Size_, 0, Array< Value_, Size_ > > {
73+
using MaskType = Mask< Value_, Size_ >; // expected-warning 0+ {{}}
74+
};
75+
76+
void test11_load_masked() {
77+
load< Array< float, 32 > >{} == 0; // expected-error 0+ {{}} expected-warning 0+ {{}} expected-note 0+ {{}}
78+
}

clang/test/SemaCXX/crash-GH78388.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -verify -std=c++98 %s
2+
// RUN: %clang_cc1 -verify -std=c++11 %s
3+
// RUN: %clang_cc1 -verify -std=c++14 %s
4+
// RUN: %clang_cc1 -verify -std=c++17 %s
5+
// RUN: %clang_cc1 -verify -std=c++20 %s
6+
// RUN: %clang_cc1 -verify -std=c++23 %s
7+
// RUN: %clang_cc1 -verify -std=c++2c %s
8+
9+
// https://github.com/llvm/llvm-project/issues/78388
10+
11+
typedef mbstate_t; // expected-error 0+ {{}} expected-note 0+ {{}}
12+
template < typename , typename , typename >
13+
class a // expected-error 0+ {{}}
14+
class b { // expected-error 0+ {{}}
15+
namespace { // expected-note 0+ {{}} expected-note 0+ {{}}
16+
template < typename c > b::operator=() { // expected-error 0+ {{}} expected-note 0+ {{}}
17+
struct :a< c, char, stdmbstate_t > d // expected-error 0+ {{}} expected-warning 0+ {{}}

libcxx/test/libcxx/utilities/expected/expected.void/transform_error.mandates.verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void test() {
5656
e.transform_error(return_unexpected<int&>); // expected-error-re@*:* {{static assertion failed {{.*}}The result of {{.*}} must be a valid template argument for unexpected}}
5757
// expected-error-re@*:* 0-1 {{{{(excess elements in struct initializer|no matching constructor for initialization of)}}{{.*}}}}
5858
// 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}}
59-
// expected-error-re@*:* {{call to deleted constructor of {{.*}}}}
59+
// expected-error-re@*:* 0-1 {{call to deleted constructor of {{.*}}}}
6060
// expected-error-re@*:* {{union member {{.*}} has reference type {{.*}}}}
6161

6262
e.transform_error(return_no_object<int&>); // expected-error-re@*:* {{static assertion failed {{.*}}The result of {{.*}} must be a valid template argument for unexpected}}

0 commit comments

Comments
 (0)