Skip to content

Commit 2f72a75

Browse files
committed
In nothrow new-expressions, null-check the result if we're going to
apply sanitizers to it. This avoids a sanitizer false positive that we are initializing a null pointer. llvm-svn: 350779
1 parent 2eeade1 commit 2f72a75

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,9 +1656,10 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
16561656
// Emit a null check on the allocation result if the allocation
16571657
// function is allowed to return null (because it has a non-throwing
16581658
// exception spec or is the reserved placement new) and we have an
1659-
// interesting initializer.
1659+
// interesting initializer will be running sanitizers on the initialization.
16601660
bool nullCheck = E->shouldNullCheckAllocation() &&
1661-
(!allocType.isPODType(getContext()) || E->hasInitializer());
1661+
(!allocType.isPODType(getContext()) || E->hasInitializer() ||
1662+
sanitizePerformTypeCheck());
16621663

16631664
llvm::BasicBlock *nullCheckBB = nullptr;
16641665
llvm::BasicBlock *contBB = nullptr;

clang/test/CodeGenCXX/catch-undef-behavior.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,49 @@ void upcast_to_vbase() {
520520
}
521521
}
522522

523+
struct nothrow {};
524+
void *operator new[](__SIZE_TYPE__, nothrow) noexcept;
525+
526+
namespace NothrowNew {
527+
struct X { X(); };
528+
529+
// CHECK-LABEL: define{{.*}}nothrow_new_trivial
530+
void *nothrow_new_trivial() {
531+
// CHECK: %[[is_null:.*]] = icmp eq i8*{{.*}}, null
532+
// CHECK: br i1 %[[is_null]], label %[[null:.*]], label %[[nonnull:.*]]
533+
534+
// CHECK: [[nonnull]]:
535+
// CHECK: llvm.objectsize
536+
// CHECK: br i1
537+
//
538+
// CHECK: call {{.*}}__ubsan_handle_type_mismatch
539+
//
540+
// CHECK: [[null]]:
541+
// CHECK-NOT: {{ }}br{{ }}
542+
// CHECK: ret
543+
return new (nothrow{}) char[123456];
544+
}
545+
546+
// CHECK-LABEL: define{{.*}}nothrow_new_nontrivial
547+
void *nothrow_new_nontrivial() {
548+
// CHECK: %[[is_null:.*]] = icmp eq i8*{{.*}}, null
549+
// CHECK: br i1 %[[is_null]], label %[[null:.*]], label %[[nonnull:.*]]
550+
551+
// CHECK: [[nonnull]]:
552+
// CHECK: llvm.objectsize
553+
// CHECK: br i1
554+
//
555+
// CHECK: call {{.*}}__ubsan_handle_type_mismatch
556+
//
557+
// CHECK: call {{.*}}_ZN10NothrowNew1XC1Ev
558+
//
559+
// CHECK: [[null]]:
560+
// CHECK-NOT: {{ }}br{{ }}
561+
// CHECK: ret
562+
return new (nothrow{}) X[123456];
563+
}
564+
}
565+
523566
struct ThisAlign {
524567
void this_align_lambda();
525568
void this_align_lambda_2();

0 commit comments

Comments
 (0)