]> granicus.if.org Git - clang/commitdiff
Fix broken diagnostic when returning the address of a stack-allocated array.
authorTed Kremenek <kremenek@apple.com>
Sat, 9 Jan 2010 20:05:00 +0000 (20:05 +0000)
committerTed Kremenek <kremenek@apple.com>
Sat, 9 Jan 2010 20:05:00 +0000 (20:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93071 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ReturnStackAddressChecker.cpp
test/Analysis/stack-addr-ps.c

index 3a6d8a41c0691ff69d6717ddfd22ec01cfc50815..4d7e8ade98f775fa99113558c1e73f983c7add37 100644 (file)
@@ -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<CompoundLiteralRegion>(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<VarRegion>(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);
   
index e58c7804ddbac70c94f73679d4d3b12dd65f0eda..315b9007d88928fcf4ea2b3f7ba4e0446c9e9165 100644 (file)
@@ -68,3 +68,10 @@ ComparatorBlock test_return_block_neg(void) {
   return b; // no-warning
 }
 
+// <rdar://problem/7523821>
+int *rdar_7523821_f2() {
+  int a[3];
+  return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}}
+};
+
+