[clang] 9acd61e - [Sema] Fix crash in __datasizeof with unknown types (#80300)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 1 08:09:37 PST 2024


Author: Ilya Biryukov
Date: 2024-02-01T17:09:32+01:00
New Revision: 9acd61ec1999ac3a54371d0a8b9d922ef5ca2b50

URL: https://github.com/llvm/llvm-project/commit/9acd61ec1999ac3a54371d0a8b9d922ef5ca2b50
DIFF: https://github.com/llvm/llvm-project/commit/9acd61ec1999ac3a54371d0a8b9d922ef5ca2b50.diff

LOG: [Sema] Fix crash in __datasizeof with unknown types (#80300)

Fixes #80284.

Calling `getASTRecordLayout` on invalid types may crash and results of
`__datasizeof` on invalid types can be arbitrary, so just use whatever
`sizeof` returns.

Added: 
    

Modified: 
    clang/lib/AST/ASTContext.cpp
    clang/test/SemaCXX/datasizeof.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 71c9c0003d18c..78a04b4c69426 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1749,7 +1749,8 @@ TypeInfoChars ASTContext::getTypeInfoDataSizeInChars(QualType T) const {
   // of a base-class subobject.  We decide whether that's possible
   // during class layout, so here we can just trust the layout results.
   if (getLangOpts().CPlusPlus) {
-    if (const auto *RT = T->getAs<RecordType>()) {
+    if (const auto *RT = T->getAs<RecordType>();
+        RT && !RT->getDecl()->isInvalidDecl()) {
       const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl());
       Info.Width = layout.getDataSize();
     }

diff  --git a/clang/test/SemaCXX/datasizeof.cpp b/clang/test/SemaCXX/datasizeof.cpp
index f96660d2028d0..5baf2ecb24ed7 100644
--- a/clang/test/SemaCXX/datasizeof.cpp
+++ b/clang/test/SemaCXX/datasizeof.cpp
@@ -51,3 +51,11 @@ struct S {
 };
 
 static_assert(S{}.i == 9);
+
+namespace GH80284 {
+struct Bar; // expected-note{{forward declaration}}
+struct Foo {
+  Bar x; // expected-error{{field has incomplete type}}
+};
+constexpr int a = __datasizeof(Foo);
+}


        


More information about the cfe-commits mailing list