Skip to content

[clang][bytecode] Check new/delete mismatch earlier #147732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,8 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
if (!CheckDynamicMemoryAllocation(S, OpPC))
return false;

DynamicAllocator &Allocator = S.getAllocator();

const Expr *Source = nullptr;
const Block *BlockToDelete = nullptr;
{
Expand All @@ -1212,6 +1214,21 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
while (Ptr.isBaseClass())
Ptr = Ptr.getBase();

Source = Ptr.getDeclDesc()->asExpr();
BlockToDelete = Ptr.block();

// Check that new[]/delete[] or new/delete were used, not a mixture.
const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
if (std::optional<DynamicAllocator::Form> AllocForm =
Allocator.getAllocationForm(Source)) {
DynamicAllocator::Form DeleteForm =
DeleteIsArrayForm ? DynamicAllocator::Form::Array
: DynamicAllocator::Form::NonArray;
if (!CheckNewDeleteForms(S, OpPC, *AllocForm, DeleteForm, BlockDesc,
Source))
return false;
}

// For the non-array case, the types must match if the static type
// does not have a virtual destructor.
if (!DeleteIsArrayForm && Ptr.getType() != InitialType &&
Expand All @@ -1230,9 +1247,6 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
return false;
}

Source = Ptr.getDeclDesc()->asExpr();
BlockToDelete = Ptr.block();

if (!CheckDeleteSource(S, OpPC, Source, Ptr))
return false;

Expand Down Expand Up @@ -1266,24 +1280,14 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
if (!RunDestructors(S, OpPC, BlockToDelete))
return false;

DynamicAllocator &Allocator = S.getAllocator();
const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
std::optional<DynamicAllocator::Form> AllocForm =
Allocator.getAllocationForm(Source);

if (!Allocator.deallocate(Source, BlockToDelete, S)) {
// Nothing has been deallocated, this must be a double-delete.
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_double_delete);
return false;
}

assert(AllocForm);
DynamicAllocator::Form DeleteForm = DeleteIsArrayForm
? DynamicAllocator::Form::Array
: DynamicAllocator::Form::NonArray;
return CheckNewDeleteForms(S, OpPC, *AllocForm, DeleteForm, BlockDesc,
Source);
return true;
}

void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
Expand Down
14 changes: 14 additions & 0 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ namespace Arrays {
}
static_assert(mismatch2() == 6); // both-error {{not an integral constant expression}} \
// both-note {{in call to 'mismatch2()'}}

constexpr int mismatch3() { // both-error {{never produces a constant expression}}
int a = 0;
struct S {};
struct T : S {};
T *p = new T[3]{}; // both-note 2{{heap allocation performed here}}
delete (S*)p; // both-note 2{{non-array delete used to delete pointer to array object of type 'T[3]'}}

return 0;

}
static_assert(mismatch3() == 0); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}

/// Array of composite elements.
constexpr int foo() {
S *ss = new S[12];
Expand Down