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

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 17 04:39:15 PST 2025


================
@@ -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())))
----------------
usx95 wrote:

Since this is on GSL init path, the case mentioned should not be affected.

I also there are related tests already present:
```
std::unique_ptr<int> getUniquePtr();
int *danglingUniquePtrFromTemp() {
  return getUniquePtr().get(); // expected-warning {{returning address of local temporary object}}
}
int *danglingUniquePtrFromTemp2() {
  return std::unique_ptr<int>().get(); // expected-warning {{returning address of local temporary object}}
}
```
So this should be good to go.

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


More information about the cfe-commits mailing list