Skip to content

Commit

Permalink
[LifetimeAnalysis] Support std::stack::top() and std::optional::value()
Browse files Browse the repository at this point in the history
Summary: Diagnose dangling pointers that come from std::stack::top() and std::optional::value().

Reviewers: gribozavr

Subscribers: cfe-commits, xazax.hun

Tags: #clang

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

llvm-svn: 368929
  • Loading branch information
mgehre committed Aug 14, 2019
1 parent 4395b31 commit 1bebc22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaInit.cpp
Expand Up @@ -6610,7 +6610,7 @@ static bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee) {
OO == OverloadedOperatorKind::OO_Star;
}
return llvm::StringSwitch<bool>(Callee->getName())
.Cases("front", "back", "at", true)
.Cases("front", "back", "at", "top", "value", true)
.Default(false);
}
return false;
Expand Down
27 changes: 23 additions & 4 deletions clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
Expand Up @@ -170,7 +170,15 @@ template<typename T>
struct optional {
optional();
optional(const T&);
T &operator*();
T &operator*() &;
T &&operator*() &&;
T &value() &;
T &&value() &&;
};

template<typename T>
struct stack {
T &top();
};
}

Expand All @@ -188,6 +196,16 @@ const char *danglingRawPtrFromLocal() {
return s.c_str(); // expected-warning {{address of stack memory associated with local variable 's' returned}}
}

int &danglingRawPtrFromLocal2() {
std::optional<int> o;
return o.value(); // expected-warning {{reference to stack memory associated with local variable 'o' returned}}
}

int &danglingRawPtrFromLocal3() {
std::optional<int> o;
return *o; // expected-warning {{reference to stack memory associated with local variable 'o' returned}}
}

const char *danglingRawPtrFromTemp() {
return std::basic_string<char>().c_str(); // expected-warning {{returning address of local temporary object}}
}
Expand All @@ -203,9 +221,10 @@ int *danglingUniquePtrFromTemp2() {
}

void danglingReferenceFromTempOwner() {
int &r = *std::optional<int>(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
int &r2 = *std::optional<int>(5); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
int &r3 = std::vector<int>().at(3); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
int &&r = *std::optional<int>(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
int &&r2 = *std::optional<int>(5); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
int &&r3 = std::optional<int>(5).value(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
int &r4 = std::vector<int>().at(3); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
}

std::vector<int> getTempVec();
Expand Down

0 comments on commit 1bebc22

Please sign in to comment.