Skip to content

[clang][bytecode] Diagnose delete of non-heap-allocated blocks #137475

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
Apr 27, 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
7 changes: 7 additions & 0 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,13 @@ static bool interp__builtin_operator_delete(InterpState &S, CodePtr OpPC,

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

if (!BlockToDelete->isDynamic()) {
S.FFDiag(Call, diag::note_constexpr_delete_not_heap_alloc)
<< Ptr.toDiagnosticString(S.getASTContext());
if (const auto *D = Ptr.getFieldDesc()->asDecl())
S.Note(D->getLocation(), diag::note_declared_at);
}
}
assert(BlockToDelete);

Expand Down
7 changes: 6 additions & 1 deletion clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ namespace std {
}
constexpr void deallocate(void *p) {
__builtin_operator_delete(p); // both-note 2{{std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}} \
// both-note {{used to delete a null pointer}}
// both-note {{used to delete a null pointer}} \
// both-note {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
}
};
template<typename T, typename ...Args>
Expand Down Expand Up @@ -1004,6 +1005,10 @@ namespace WrongFrame {

}

constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&no_deallocate_nonalloc), 1); // both-error {{constant expression}} \
// both-note {{in call}} \
// both-note {{declared here}}

#else
/// Make sure we reject this prior to C++20
constexpr int a() { // both-error {{never produces a constant expression}}
Expand Down
Loading