diff options
author | John McCall <rjmccall@apple.com> | 2013-03-27 00:03:48 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2013-03-27 00:03:48 +0000 |
commit | f2abe19dbb0c34fdfe7d3402c091c00982cdc61c (patch) | |
tree | f53fd35576b837d5a6f2a56e95cd20b7b35c9c85 | |
parent | 65d635775d986105a2cebef6a1ed33997695c055 (diff) | |
download | bcm5719-llvm-f2abe19dbb0c34fdfe7d3402c091c00982cdc61c.tar.gz bcm5719-llvm-f2abe19dbb0c34fdfe7d3402c091c00982cdc61c.zip |
Make the -Wreinterpret-base-class logic safe against invalid
declarations at any point. Patch by Alexander Zinenko, and
report by Richard Smith.
llvm-svn: 178098
-rw-r--r-- | clang/lib/Sema/SemaCast.cpp | 21 | ||||
-rw-r--r-- | clang/test/SemaCXX/warn-reinterpret-base-class.cpp | 47 |
2 files changed, 62 insertions, 6 deletions
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp index 1dbc9aaca59..0e19adfd28c 100644 --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -695,14 +695,15 @@ static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr, const CXXRecordDecl *SrcRD = SrcPointeeRD ? SrcPointeeRD : SrcType->getAsCXXRecordDecl(); - // Examining subobjects for records is only possible if the complete - // definition is available. Also, template instantiation is not allowed here. - if(!SrcRD || !SrcRD->isCompleteDefinition()) + // Examining subobjects for records is only possible if the complete and + // valid definition is available. Also, template instantiation is not + // allowed here. + if (!SrcRD || !SrcRD->isCompleteDefinition() || SrcRD->isInvalidDecl()) return; const CXXRecordDecl *DestRD = DestType->getPointeeCXXRecordDecl(); - if(!DestRD || !DestRD->isCompleteDefinition()) + if (!DestRD || !DestRD->isCompleteDefinition() || DestRD->isInvalidDecl()) return; enum { @@ -721,7 +722,7 @@ static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr, bool VirtualBase = true; bool NonZeroOffset = false; - for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(), + for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(), E = BasePaths.end(); I != E; ++I) { const CXXBasePath &Path = *I; @@ -734,8 +735,16 @@ static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr, break; const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl(); assert(BaseRD && "Base type should be a valid unqualified class type"); + // Don't check if any base has invalid declaration or has no definition + // since it has no layout info. + const CXXRecordDecl *Class = IElem->Class, + *ClassDefinition = Class->getDefinition(); + if (Class->isInvalidDecl() || !ClassDefinition || + !ClassDefinition->isCompleteDefinition()) + return; + const ASTRecordLayout &DerivedLayout = - Self.Context.getASTRecordLayout(IElem->Class); + Self.Context.getASTRecordLayout(Class); Offset += DerivedLayout.getBaseClassOffset(BaseRD); } if (!IsVirtual) { diff --git a/clang/test/SemaCXX/warn-reinterpret-base-class.cpp b/clang/test/SemaCXX/warn-reinterpret-base-class.cpp index fc7d15c8a34..a7deafeadf2 100644 --- a/clang/test/SemaCXX/warn-reinterpret-base-class.cpp +++ b/clang/test/SemaCXX/warn-reinterpret-base-class.cpp @@ -37,6 +37,53 @@ void reinterpret_not_defined_class(B *b, C *c) { (void)reinterpret_cast<B &>(*c); } +// Do not fail on erroneous classes with fields of incompletely-defined types. +// Base class is malformed. +namespace BaseMalformed { + struct A; // expected-note {{forward declaration of 'BaseMalformed::A'}} + struct B { + A a; // expected-error {{field has incomplete type 'BaseMalformed::A'}} + }; + struct C : public B {} c; + B *b = reinterpret_cast<B *>(&c); +} // end anonymous namespace + +// Child class is malformed. +namespace ChildMalformed { + struct A; // expected-note {{forward declaration of 'ChildMalformed::A'}} + struct B {}; + struct C : public B { + A a; // expected-error {{field has incomplete type 'ChildMalformed::A'}} + } c; + B *b = reinterpret_cast<B *>(&c); +} // end anonymous namespace + +// Base class outside upcast base-chain is malformed. +namespace BaseBaseMalformed { + struct A; // expected-note {{forward declaration of 'BaseBaseMalformed::A'}} + struct Y {}; + struct X { A a; }; // expected-error {{field has incomplete type 'BaseBaseMalformed::A'}} + struct B : Y, X {}; + struct C : B {} c; + B *p = reinterpret_cast<B*>(&c); +} + +namespace InheritanceMalformed { + struct A; // expected-note {{forward declaration of 'InheritanceMalformed::A'}} + struct B : A {}; // expected-error {{base class has incomplete type}} + struct C : B {} c; + B *p = reinterpret_cast<B*>(&c); +} + +// Virtual base class outside upcast base-chain is malformed. +namespace VBaseMalformed{ + struct A; // expected-note {{forward declaration of 'VBaseMalformed::A'}} + struct X { A a; }; // expected-error {{field has incomplete type 'VBaseMalformed::A'}} + struct B : public virtual X {}; + struct C : B {} c; + B *p = reinterpret_cast<B*>(&c); +} + void reinterpret_not_updowncast(A *pa, const A *pca, A &a, const A &ca) { (void)*reinterpret_cast<C *>(pa); (void)*reinterpret_cast<const C *>(pa); |