Skip to content

Commit 6b57839

Browse files
hokeintstellar
authored andcommitted
[clang] Fix false positive regression for lifetime analysis warning. (llvm#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)
1 parent f567c03 commit 6b57839

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,11 +1239,12 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
12391239
}
12401240
// Check the return type, e.g.
12411241
// const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1242+
// GSLOwner* func(cosnt Foo& foo [[clang::lifetimebound]])
12421243
// GSLPointer func(const Foo& foo [[clang::lifetimebound]])
12431244
if (FD &&
1244-
((FD->getReturnType()->isReferenceType() &&
1245+
((FD->getReturnType()->isPointerOrReferenceType() &&
12451246
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType())) ||
1246-
isPointerLikeType(FD->getReturnType())))
1247+
isGLSPointerType(FD->getReturnType())))
12471248
return Report;
12481249

12491250
return Abandon;

clang/test/Sema/Inputs/lifetime-analysis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct basic_string_view {
6161
basic_string_view();
6262
basic_string_view(const T *);
6363
const T *begin() const;
64+
const T *data() const;
6465
};
6566
using string_view = basic_string_view<char>;
6667

@@ -80,6 +81,7 @@ struct basic_string {
8081
const T *c_str() const;
8182
operator basic_string_view<T> () const;
8283
using const_iterator = iter<T>;
84+
const T *data() const;
8385
};
8486
using string = basic_string<char>;
8587

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,27 @@ struct Test {
852852
};
853853

854854
} // namespace GH120543
855+
856+
namespace GH127195 {
857+
template <typename T>
858+
struct StatusOr {
859+
T* operator->() [[clang::lifetimebound]];
860+
T* value() [[clang::lifetimebound]];
861+
};
862+
863+
const char* foo() {
864+
StatusOr<std::string> s;
865+
return s->data(); // expected-warning {{address of stack memory associated with local variable}}
866+
867+
StatusOr<std::string_view> s2;
868+
return s2->data();
869+
870+
StatusOr<StatusOr<std::string_view>> s3;
871+
return s3.value()->value()->data();
872+
873+
// FIXME: nested cases are not supported now.
874+
StatusOr<StatusOr<std::string>> s4;
875+
return s4.value()->value()->data();
876+
}
877+
878+
} // namespace GH127195

0 commit comments

Comments
 (0)