Skip to content

Commit 9acd61e

Browse files
[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.
1 parent 6ac4fe8 commit 9acd61e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,8 @@ TypeInfoChars ASTContext::getTypeInfoDataSizeInChars(QualType T) const {
17491749
// of a base-class subobject. We decide whether that's possible
17501750
// during class layout, so here we can just trust the layout results.
17511751
if (getLangOpts().CPlusPlus) {
1752-
if (const auto *RT = T->getAs<RecordType>()) {
1752+
if (const auto *RT = T->getAs<RecordType>();
1753+
RT && !RT->getDecl()->isInvalidDecl()) {
17531754
const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl());
17541755
Info.Width = layout.getDataSize();
17551756
}

clang/test/SemaCXX/datasizeof.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ struct S {
5151
};
5252

5353
static_assert(S{}.i == 9);
54+
55+
namespace GH80284 {
56+
struct Bar; // expected-note{{forward declaration}}
57+
struct Foo {
58+
Bar x; // expected-error{{field has incomplete type}}
59+
};
60+
constexpr int a = __datasizeof(Foo);
61+
}

0 commit comments

Comments
 (0)