From: Ted Kremenek Date: Sat, 9 Jan 2010 20:05:00 +0000 (+0000) Subject: Fix broken diagnostic when returning the address of a stack-allocated array. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7960ec30d794da5de6cd017c728e1151f7b101b9;p=clang Fix broken diagnostic when returning the address of a stack-allocated array. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93071 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/ReturnStackAddressChecker.cpp b/lib/Analysis/ReturnStackAddressChecker.cpp index 3a6d8a41c0..4d7e8ade98 100644 --- a/lib/Analysis/ReturnStackAddressChecker.cpp +++ b/lib/Analysis/ReturnStackAddressChecker.cpp @@ -67,6 +67,9 @@ void ReturnStackAddressChecker::PreVisitReturnStmt(CheckerContext &C, llvm::raw_svector_ostream os(buf); SourceRange range; + // Get the base region, stripping away fields and elements. + R = R->getBaseRegion(); + // Check if the region is a compound literal. if (const CompoundLiteralRegion* CR = dyn_cast(R)) { const CompoundLiteralExpr* CL = CR->getLiteralExpr(); @@ -92,13 +95,18 @@ void ReturnStackAddressChecker::PreVisitReturnStmt(CheckerContext &C, << C.getSourceManager().getInstantiationLineNumber(L) << " returned to caller"; } - else { + else if (const VarRegion *VR = dyn_cast(R)) { os << "Address of stack memory associated with local variable '" - << R->getString() << "' returned."; + << VR->getString() << "' returned"; + range = VR->getDecl()->getSourceRange(); + } + else { + assert(false && "Invalid region in ReturnStackAddressChecker."); + return; } RangedBugReport *report = new RangedBugReport(*BT, os.str(), N); - report->addRange(RS->getSourceRange()); + report->addRange(RetE->getSourceRange()); if (range.isValid()) report->addRange(range); diff --git a/test/Analysis/stack-addr-ps.c b/test/Analysis/stack-addr-ps.c index e58c7804dd..315b9007d8 100644 --- a/test/Analysis/stack-addr-ps.c +++ b/test/Analysis/stack-addr-ps.c @@ -68,3 +68,10 @@ ComparatorBlock test_return_block_neg(void) { return b; // no-warning } +// +int *rdar_7523821_f2() { + int a[3]; + return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}} +}; + +