Skip to content

Commit

Permalink
[Sema] Fix crash in __datasizeof with unknown types (#80300)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilya-biryukov committed Feb 1, 2024
1 parent 6ac4fe8 commit 9acd61e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 8 additions & 0 deletions clang/test/SemaCXX/datasizeof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 9acd61e

Please sign in to comment.