vsk created this revision.
vsk added reviewers: erik.pilkington, delcypher.
Herald added a subscriber: dexonsmith.

Null-check a TypeLoc before casting it to a FunctionTypeLoc. This fixes
a crash in -fsanitize=nullability-return.

rdar://59263039


https://reviews.llvm.org/D74355

Files:
  clang/lib/CodeGen/CGCall.cpp
  clang/test/CodeGenObjC/ubsan-nullability-return-notypeloc.m


Index: clang/test/CodeGenObjC/ubsan-nullability-return-notypeloc.m
===================================================================
--- /dev/null
+++ clang/test/CodeGenObjC/ubsan-nullability-return-notypeloc.m
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsanitize=nullability-return -emit-llvm %s -o - -triple 
x86_64-apple-macosx10.10.0 | FileCheck %s
+
+// CHECK-LABEL: define i8* @foo()
+// CHECK: call i8* @helper()
+// CHECK-NEXT: ret i8*
+
+#pragma clang assume_nonnull begin
+__attribute__((ns_returns_retained)) id foo(void) {
+  extern id helper(void);
+  return helper();
+}
+#pragma clang assume_nonnull end
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -3060,7 +3060,7 @@
   } else {
     if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
       if (auto *TSI = DD->getTypeSourceInfo())
-        if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>())
+        if (auto FTL = TSI->getTypeLoc().getAs<FunctionTypeLoc>())
           AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
     CheckKind = SanitizerKind::NullabilityReturn;
     Handler = SanitizerHandler::NullabilityReturn;


Index: clang/test/CodeGenObjC/ubsan-nullability-return-notypeloc.m
===================================================================
--- /dev/null
+++ clang/test/CodeGenObjC/ubsan-nullability-return-notypeloc.m
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsanitize=nullability-return -emit-llvm %s -o - -triple x86_64-apple-macosx10.10.0 | FileCheck %s
+
+// CHECK-LABEL: define i8* @foo()
+// CHECK: call i8* @helper()
+// CHECK-NEXT: ret i8*
+
+#pragma clang assume_nonnull begin
+__attribute__((ns_returns_retained)) id foo(void) {
+  extern id helper(void);
+  return helper();
+}
+#pragma clang assume_nonnull end
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -3060,7 +3060,7 @@
   } else {
     if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
       if (auto *TSI = DD->getTypeSourceInfo())
-        if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>())
+        if (auto FTL = TSI->getTypeLoc().getAs<FunctionTypeLoc>())
           AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
     CheckKind = SanitizerKind::NullabilityReturn;
     Handler = SanitizerHandler::NullabilityReturn;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to