Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ class FindStackRegionsSymbolVisitor final : public SymbolVisitor {
void SaveIfEscapes(const MemRegion *MR) {
const StackSpaceRegion *SSR =
MR->getMemorySpace()->getAs<StackSpaceRegion>();
if (SSR && SSR->getStackFrame() == PoppedStackFrame)

if (!SSR)
return;

const StackFrameContext *CapturedSFC = SSR->getStackFrame();
if (CapturedSFC == PoppedStackFrame ||
PoppedStackFrame->isParentOf(CapturedSFC))
EscapingStackRegions.push_back(MR);
}

Expand Down
45 changes: 45 additions & 0 deletions clang/test/Analysis/stack-addr-ps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,51 @@ int& ret_local_field_ref() {
}
} //namespace return_address_of_true_positives

namespace return_from_child_block_scope {
struct S {
int *p;
};

S return_child_stack_context() {
S s;
{
int a = 1;
s = (S){ &a };
}
return s; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
}

S return_child_stack_context_field() {
S s;
{
int a = 1;
s.p = &a;
}
return s; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
}

// The below are reproducers from Issue #123459
template <typename V>
struct T {
V* q{};
T() = default;
T(T&& rhs) { q = rhs.q; rhs.q = nullptr;}
T& operator=(T&& rhs) { q = rhs.q; rhs.q = nullptr;}
void push_back(const V& v) { if (q == nullptr) q = new V(v); }
~T() { delete q; }
};

T<S> f() {
T<S> t;
{
int a = 1;
t.push_back({ &a });
}
return t; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
}

} // namespace return_from_child_block_scope

namespace true_negatives_return_expressions {
struct Container { int *x; };

Expand Down
22 changes: 22 additions & 0 deletions clang/test/Analysis/stackaddrleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,25 @@ int *g_no_lifetime_bound() {
int i = 0;
return f_no_lifetime_bound(&i); // no-warning
}

struct child_stack_context_s {
int *p;
};

struct child_stack_context_s return_child_stack_context() {
struct child_stack_context_s s;
{
int a = 1;
s = (struct child_stack_context_s){ &a };
}
return s; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
}

struct child_stack_context_s return_child_stack_context_field() {
struct child_stack_context_s s;
{
int a = 1;
s.p = &a;
}
return s; // expected-warning {{Address of stack memory associated with local variable 'a' returned to caller}}
}