llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-analysis

@llvm/pr-subscribers-clang-temporal-safety

Author: Utkarsh Saxena (usx95)

<details>
<summary>Changes</summary>

Fix lifetime tracking for pointer-like methods on non-owner types

Modified the `shouldTrackImplicitObjectArg` function to only track pointer-like 
return types when the function object parameter is a GSL owner type. This 
prevents incorrectly tracking methods on view types like 
`std::string_view::begin()`.

Also updated a test case to remove an expected warning that is no longer 
applicable, and added a comment explaining why we can't catch certain bugs with 
view type accessors.

The current implementation was incorrectly tracking pointer-like return types 
for all GSL pointer types, including view types. This change makes the tracking 
more precise by only tracking pointer-like return types when the function 
object parameter is a GSL owner type, which better aligns with the intended 
behavior of the lifetime safety analysis.

This resolves false-positives like

```cpp
const char* p;
{
  std::string_view a = getSomeView();
  p = a.data(); // 'a' does not live long enough (false-positive).
}
use(p);
```

This also introduces a false-negative in the pre-existing analysis which 
special cased this

```cpp
const char *trackThroughMultiplePointer() {
  return 
std::basic_string_view&lt;char&gt;(std::basic_string&lt;char&gt;()).begin();
}
```

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


2 Files Affected:

- (modified) clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp (+2-1) 
- (modified) clang/test/Sema/warn-lifetime-analysis-nocfg.cpp (+3-1) 


``````````diff
diff --git a/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp 
b/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
index 2772fe20de19b..f6afc43843a82 100644
--- a/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
@@ -99,7 +99,8 @@ bool shouldTrackImplicitObjectArg(const CXXMethodDecl 
*Callee) {
   if (!isGslPointerType(Callee->getFunctionObjectParameterType()) &&
       !isGslOwnerType(Callee->getFunctionObjectParameterType()))
     return false;
-  if (isPointerLikeType(Callee->getReturnType())) {
+  if (isPointerLikeType(Callee->getReturnType()) &&
+      isGslOwnerType(Callee->getFunctionObjectParameterType())) {
     if (!Callee->getIdentifier())
       return false;
     return llvm::StringSwitch<bool>(Callee->getName())
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp 
b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 7634dbf2f6733..b04ed86cd8abd 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -249,8 +249,10 @@ int &doNotFollowReferencesForLocalOwner() {
   return p; // ok
 }
 
+// It is not possible to annotate accessors of view types 
+// (like std::string_view::begin()) to catch bugs like the following:
 const char *trackThroughMultiplePointer() {
-  return std::basic_string_view<char>(std::basic_string<char>()).begin(); // 
expected-warning {{returning address of local temporary object}}
+  return std::basic_string_view<char>(std::basic_string<char>()).begin();
 }
 
 struct X {

``````````

</details>


https://github.com/llvm/llvm-project/pull/174741
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to