[CodeGen] bugfix: ApplyDebugLocation goes out of scope before intended
authorusama hameed <u_hameed@apple.com>
Fri, 20 Jan 2023 19:24:05 +0000 (11:24 -0800)
committerusama hameed <u_hameed@apple.com>
Tue, 24 Jan 2023 23:31:07 +0000 (15:31 -0800)
rdar://103570533

Differential Revision: https://reviews.llvm.org/D142243

clang/lib/CodeGen/CodeGenFunction.cpp
clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m [new file with mode: 0644]

index 55464e1..8cbe2a5 100644 (file)
@@ -361,17 +361,18 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
   bool HasOnlyLifetimeMarkers =
       HasCleanups && EHStack.containsOnlyLifetimeMarkers(PrologueCleanupDepth);
   bool EmitRetDbgLoc = !HasCleanups || HasOnlyLifetimeMarkers;
+
+  std::optional<ApplyDebugLocation> OAL;
   if (HasCleanups) {
     // Make sure the line table doesn't jump back into the body for
     // the ret after it's been at EndLoc.
-    std::optional<ApplyDebugLocation> AL;
     if (CGDebugInfo *DI = getDebugInfo()) {
       if (OnlySimpleReturnStmts)
         DI->EmitLocation(Builder, EndLoc);
       else
         // We may not have a valid end location. Try to apply it anyway, and
         // fall back to an artificial location if needed.
-        AL = ApplyDebugLocation::CreateDefaultArtificial(*this, EndLoc);
+        OAL = ApplyDebugLocation::CreateDefaultArtificial(*this, EndLoc);
     }
 
     PopCleanupBlocks(PrologueCleanupDepth);
diff --git a/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m b/clang/test/CodeGenObjC/objc-arc-ubsan-debugging.m
new file mode 100644 (file)
index 0000000..531f999
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: %clang -x objective-c -target arm64-apple-macos12.0 -fobjc-arc -std=gnu99  -O0 -fsanitize=undefined -fsanitize=nullability -c %s -v -g
+
+@interface NSString
+@end
+
+struct A {
+    NSString *a;
+};
+
+NSString* _Nonnull foo()
+{
+    struct A a;
+    return 0;
+}