Skip to content

Commit 8b81ebf

Browse files
committed
[ubsan] Null-check and adjust TypeLoc before using it
Null-check and adjut a TypeLoc before casting it to a FunctionTypeLoc. This fixes a crash in -fsanitize=nullability-return, and also makes the location of the nonnull type available when the return type is adjusted. rdar://59263039 Differential Revision: https://reviews.llvm.org/D74355
1 parent 7683a08 commit 8b81ebf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3060,7 +3060,7 @@ void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {
30603060
} else {
30613061
if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
30623062
if (auto *TSI = DD->getTypeSourceInfo())
3063-
if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>())
3063+
if (auto FTL = TSI->getTypeLoc().getAsAdjusted<FunctionTypeLoc>())
30643064
AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
30653065
CheckKind = SanitizerKind::NullabilityReturn;
30663066
Handler = SanitizerHandler::NullabilityReturn;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -fsanitize=nullability-return -emit-llvm %s -o - -triple x86_64-apple-macosx10.10.0 | FileCheck %s
2+
3+
// CHECK: [[ATTR_LOC:@[0-9]+]] = {{.*}} global { {{.*}} i32 15, i32 38
4+
5+
// CHECK-LABEL: define i8* @_Z3foov()
6+
// CHECK: [[CALL:%.*]] = call i8* @_Z6helperv()
7+
// CHECK: icmp ne i8* [[CALL]]
8+
// CHECK: call void @__ubsan_handle_nullability_return_v1_abort({{.*}}[[ATTR_LOC]]
9+
10+
struct S {
11+
using PtrTy = id;
12+
};
13+
14+
#pragma clang assume_nonnull begin
15+
__attribute__((ns_returns_retained)) S::PtrTy foo(void) {
16+
extern S::PtrTy helper(void);
17+
return helper();
18+
}
19+
#pragma clang assume_nonnull end

0 commit comments

Comments
 (0)