[PATCH] D136018: [Clang] Fix crash when checking misaligned member with dependent type

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 15 06:33:34 PDT 2022


junaire created this revision.
junaire added reviewers: aaron.ballman, shafik.
Herald added a project: All.
junaire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If the type is dependent, we should just discard it and not checking its
alignment as it doesn't exisit yet.
Fixes https://github.com/llvm/llvm-project/issues/58370


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136018

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp


Index: clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/misaligned-member-with-depdent-type.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+struct __attribute__((packed)) {
+  unsigned options;
+  template <typename T>
+  void getOptions() {
+      (T *)&options;
+  }
+} s;
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -17393,12 +17393,13 @@
       cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) {
     auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
     if (isa<MemberExpr>(Op)) {
-      auto MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
+      auto *MA = llvm::find(MisalignedMembers, MisalignedMember(Op));
+      const bool IsDiscardMisalignedPointer =
+          T->isPointerType() &&
+          (T->getPointeeType()->isIncompleteType() || T->isDependentType() ||
+           Context.getTypeAlignInChars(T->getPointeeType()) <= MA->Alignment);
       if (MA != MisalignedMembers.end() &&
-          (T->isIntegerType() ||
-           (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
-                                   Context.getTypeAlignInChars(
-                                       T->getPointeeType()) <= MA->Alignment))))
+          (T->isIntegerType() || IsDiscardMisalignedPointer))
         MisalignedMembers.erase(MA);
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136018.468014.patch
Type: text/x-patch
Size: 1602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221015/517c9c96/attachment.bin>


More information about the cfe-commits mailing list