Fix crash parsing invalid code
authorOlivier Goffart <ogoffart@woboq.com>
Fri, 7 Jul 2017 09:38:59 +0000 (09:38 +0000)
committerOlivier Goffart <ogoffart@woboq.com>
Fri, 7 Jul 2017 09:38:59 +0000 (09:38 +0000)
commit67049f034da014ad51f1e5a09fbb97a86925c31f
tree4051ae561eeea3b659d14cb63ea5353190983f39
parent1b51b7fc7f99f1d291284d83aaa8bcc4ce5fd62c
Fix crash parsing invalid code

The code in the test caused a crash with this backtrace:

 RecordLayoutBuilder.cpp:2934: const clang::ASTRecordLayout &clang::ASTContext::getASTRecordLayout(const clang::RecordDecl *) const: Assertion `!D->isInvalidDecl() && "Cannot get layout of invalid decl!"' failed.
 [...]
 #7 0x00007f63963d845a __assert_fail_base (/usr/lib/libc.so.6+0x2c45a)
 #8 0x00007f63963d84d2 (/usr/lib/libc.so.6+0x2c4d2)
 #9 0x00007f63937a0631 clang::ASTContext::getASTRecordLayout(clang::RecordDecl const*) const /home/olivier/prog/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2935:3
 #10 0x00007f63937a1ad5 getFieldOffset(clang::ASTContext const&, clang::FieldDecl const*) /home/olivier/prog/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:3057:37
 #11 0x00007f6391869f14 clang::Sema::RefersToMemberWithReducedAlignment(clang::Expr*, llvm::function_ref<void (clang::Expr*, clang::RecordDecl*, clang::FieldDecl*, clang::CharUnits)>) /home/olivier/prog/llvm/tools/clang/lib/Sema/SemaChecking.cpp:12139:23
 #12 0x00007f639186a2f8 clang::Sema::CheckAddressOfPackedMember(clang::Expr*) /home/olivier/prog/llvm/tools/clang/lib/Sema/SemaChecking.cpp:12190:1
 #13 0x00007f6391a7a81c clang::Sema::CheckAddressOfOperand(clang::ActionResult<clang::Expr*, true>&, clang::SourceLocation) /home/olivier/prog/llvm/tools/clang/lib/Sema/SemaExpr.cpp:11111:10
 #14 0x00007f6391a7f5d2 clang::Sema::CreateBuiltinUnaryOp(clang::SourceLocation, clang::UnaryOperatorKind, clang::Expr*) /home/olivier/prog/llvm/tools/clang/lib/Sema/SemaExpr.cpp:11932:18

Fixing by bailing out for invalid classes.

Differential Revision: https://reviews.llvm.org/D35108

llvm-svn: 307371
clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/address-packed.c