Skip to content

Commit d9b377d

Browse files
authored
[clang][bytecode] Don't produce a null type when checking new exprs (#110252)
getType() might give us the right type already, so use that instead of calling getPointeeType() for all CXXNewExprs.
1 parent a3b34e6 commit d9b377d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,8 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
13221322
const auto *NewExpr = cast<CXXNewExpr>(E);
13231323
QualType StorageType = Ptr.getType();
13241324

1325-
if (isa_and_nonnull<CXXNewExpr>(Ptr.getFieldDesc()->asExpr())) {
1325+
if (isa_and_nonnull<CXXNewExpr>(Ptr.getFieldDesc()->asExpr()) &&
1326+
StorageType->isPointerType()) {
13261327
// FIXME: Are there other cases where this is a problem?
13271328
StorageType = StorageType->getPointeeType();
13281329
}

clang/test/AST/ByteCode/placement-new.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,18 @@ namespace ConstructAt {
271271
// both-note {{in call}}
272272

273273
}
274+
275+
namespace UsedToCrash {
276+
struct S {
277+
int* i;
278+
constexpr S() : i(new int(42)) {} // #no-deallocation
279+
constexpr ~S() {delete i;}
280+
};
281+
consteval void alloc() {
282+
S* s = new S();
283+
s->~S();
284+
new (s) S();
285+
delete s;
286+
}
287+
int alloc1 = (alloc(), 0);
288+
}

0 commit comments

Comments
 (0)