Skip to content

[clang] Fix false positive regression for lifetime analysis warning. #127460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2025

Conversation

hokein
Copy link
Collaborator

@hokein hokein commented Feb 17, 2025

This fixes a false positive caused by #114044.

For GSLPointer* types, it's less clear whether the lifetime issue is about the GSLPointer object itself or the owner it points to. To avoid false positives, we take a conservative approach in our heuristic.

Fixes #127195

(This will be backported to release 20).

@hokein hokein requested review from Xazax-hun and usx95 February 17, 2025 09:20
@hokein hokein marked this pull request as ready for review February 17, 2025 09:20
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 17, 2025

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

Changes

This fixes a false positive caused by #114044.

For GSLPointer* types, it's less clear whether the lifetime issue is about the GSLPointer object itself or the owner it points to. To avoid false positives, we take a conservative approach in our heuristic.

Fixes #127195

(This will be backported to release 20).


Full diff: https://github.com/llvm/llvm-project/pull/127460.diff

2 Files Affected:

  • (modified) clang/lib/Sema/CheckExprLifetime.cpp (+3-2)
  • (modified) clang/test/Sema/warn-lifetime-analysis-nocfg.cpp (+25)
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 8963cad86dbca..3e0867343ccee 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1239,11 +1239,12 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
     }
     // Check the return type, e.g.
     //   const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
+    //   GSLOwner* func(cosnt Foo& foo [[clang::lifetimebound]])
     //   GSLPointer func(const Foo& foo [[clang::lifetimebound]])
     if (FD &&
-        ((FD->getReturnType()->isReferenceType() &&
+        ((FD->getReturnType()->isPointerOrReferenceType() &&
           isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType())) ||
-         isPointerLikeType(FD->getReturnType())))
+          isGLSPointerType(FD->getReturnType())))
       return Report;
 
     return Abandon;
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 04bb1330ded4c..e2bad643ef012 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -852,3 +852,28 @@ struct Test {
 };
 
 } // namespace GH120543
+
+namespace GH127195 {
+template <typename T>
+struct StatusOr {
+  T* operator->() [[clang::lifetimebound]];
+  T* value() [[clang::lifetimebound]];
+};
+
+const char* foo() {
+  StatusOr<std::string> s;
+  return s->data(); // expected-warning {{address of stack memory associated with local variable}}
+  
+  StatusOr<std::string_view> s2;
+  return s2->data();
+
+  StatusOr<StatusOr<std::string_view>> s3;
+  return s3.value()->value()->data();
+
+  // would be nice to support the nested cases.
+  StatusOr<StatusOr<std::string>> s4;
+  return s4.value()->value()->data();
+}
+
+} // namespace GH127195
+  
\ No newline at end of file

Copy link

github-actions bot commented Feb 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@usx95 usx95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@hokein hokein merged commit 9c49b18 into llvm:main Feb 17, 2025
5 of 7 checks passed
@hokein hokein deleted the fix-fp branch February 17, 2025 13:40
@hokein
Copy link
Collaborator Author

hokein commented Feb 17, 2025

/cherry-pick 9c49b18

@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

/cherry-pick 9c49b18

Error: Command failed due to missing milestone.

@hokein hokein added this to the LLVM 20.X Release milestone Feb 18, 2025
@hokein
Copy link
Collaborator Author

hokein commented Feb 18, 2025

/cherry-pick 9c49b18

@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

/pull-request #127618

swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Feb 18, 2025
…lvm#127460)

This fixes a false positive caused by llvm#114044.

For `GSLPointer*` types, it's less clear whether the lifetime issue is
about the GSLPointer object itself or the owner it points to. To avoid
false positives, we take a conservative approach in our heuristic.

Fixes llvm#127195

(This will be backported to release 20).

(cherry picked from commit 9c49b18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
Development

Successfully merging this pull request may close these issues.

-Wreturn-stack-address false positive
4 participants