<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Jun 8, 2010, at 3:00 AM, Zhongxing Xu wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Author: zhongxingxu<br>Date: Tue Jun  8 05:00:00 2010<br>New Revision: 105602<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=105602&view=rev">http://llvm.org/viewvc/llvm-project?rev=105602&view=rev</a><br>Log:<br>Add a checker check if a global variable holds a local variable's address after<br>the function call is left where the local variable is declared.<br></div></blockquote><div><br></div>Awesome Zhongxing.  A few comments:</div><div><br><blockquote type="cite"><div><font class="Apple-style-span" color="#000000"><br></font>+        R = R->getBaseRegion();<br>+        if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {<br>+          const VarDecl *VD = VR->getDecl();<br>+          const DeclContext *DC = VD->getDeclContext();<br></div></blockquote><div><br></div><div>We can probably be more general and handle all stack allocated memory, including those returned from alloca().  I think checking the memory space of the region as being from the stack should be sufficient.</div><br><blockquote type="cite"><div>+          // Get the function where the variable is declared.<br>+          if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {<br>+            // Check if the function is the function we are leaving.<br>+            if (FD == LCtx->getDecl()) {<br></div></blockquote><div><br></div><div>This will leave out Objective-C methods and blocks (closures).  Don't we have a more general way of checking this by looking at the location context?  (e.g., StackFrameContext).  This is precisely what StackFrameContext was created for.  That way you can handle any stack frame, regardless of what created it (C function, Objective-C method, block, etc).</div><br><blockquote type="cite"><div>+              // The variable is declared in the function scope which we are <br>+              // leaving. Keeping this variable's address in a global variable<br>+              // is dangerous.<br>+              // FIXME: Currently VarRegion does not carry context information.<br>+              // So we cannot tell if the local variable instance is in the<br>+              // current stack frame.</div></blockquote><div><br></div><div>I don't think this is true.  I think you can look at the memory space of the VarRegion.  Is that not the case?</div><div><br></div><div>Incidentally, instead of making this a separate checker, should this just be merged in with ReturnStackAddressChecker.cpp?  They are both two sides of the same kind of error, and probably could share most of their logic.</div></div></body></html>