Skip to content

Commit 889cca3

Browse files
committed
Avoid delete nullptr in unsafe
1 parent ed2d20d commit 889cca3

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,14 +3580,16 @@ bool Converter::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
35803580
bool Converter::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
35813581
auto *argument = expr->getArgument();
35823582
auto destroyed_type = expr->getDestroyedType();
3583+
PushBrace brace(*this);
3584+
StrCat(keyword::kLet, "__p", token::kAssign, ToString(argument),
3585+
token::kSemiColon);
3586+
StrCat("if !__p.is_null()");
3587+
PushBrace not_null(*this);
35833588
if (!TypeNeedsDestruction(destroyed_type)) {
3584-
EmitDeallocation(expr, ToString(argument));
3589+
EmitDeallocation(expr, "__p");
35853590
return false;
35863591
}
35873592
auto record_name = GetRecordName(destroyed_type->getAsCXXRecordDecl());
3588-
PushBrace brace(*this);
3589-
StrCat(keyword::kLet, "__p", token::kAssign, ToString(argument),
3590-
token::kSemiColon);
35913593
if (expr->isArrayForm()) {
35923594
StrCat(std::format("for __i in 0..libcc2rs::malloc_usable_size(__p as *mut "
35933595
"::libc::c_void) / ::std::mem::size_of::<{0}>() {{ "

0 commit comments

Comments
 (0)