[PATCH] D105451: [clang] Fix crash when there is an invalid declaration with flag -Wcast-align

Queen Dela Cruz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 5 18:05:38 PDT 2021


qdelacru created this revision.
qdelacru added a reviewer: aaron.ballman.
qdelacru requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When there is unknown type in a struct in code compiled with -Wcast-align, the compiler crashes due to clang::ASTContext::getASTRecordLayout() failing an assert.

  repro.c:1:21: error: unknown type name 'doesnotexist'
  struct { int hello; doesnotexist world; } foo;
                      ^
  clang: /root/llvm-project/clang/lib/AST/RecordLayoutBuilder.cpp:3230: const clang::ASTRecordLayout& clang::ASTContext::getASTRecordLayout(const clang::RecordDecl*) const: Assertion `!D->isInvalidDecl() && "Cannot get layout of invalid decl!"' failed.

Added check that the RecordDecl is valid before calling getASTRecordLayout()


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105451

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/warn-cast-align.c


Index: clang/test/Sema/warn-cast-align.c
===================================================================
--- clang/test/Sema/warn-cast-align.c
+++ clang/test/Sema/warn-cast-align.c
@@ -67,3 +67,9 @@
 FnTy test5(void) {
   return (FnTy)&func5;
 }
+
+
+void test6() {
+    struct { int hello; doesnotexist world; } foo; // expected-error {{unknown type name 'doesnotexist'}}
+    void** repro = (void**)&foo.hello;             // expected-warning {{cast from 'int *' to 'void **' increases required alignment from 4 to 8}}             
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -14475,7 +14475,7 @@
   case Stmt::MemberExprClass: {
     auto *ME = cast<MemberExpr>(E);
     auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
-    if (!FD || FD->getType()->isReferenceType())
+    if (!FD || FD->getType()->isReferenceType() || FD->getParent()->isInvalidDecl())
       break;
     Optional<std::pair<CharUnits, CharUnits>> P;
     if (ME->isArrow())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105451.356577.patch
Type: text/x-patch
Size: 1096 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210706/94239e1e/attachment.bin>


More information about the cfe-commits mailing list