[llvm-branch-commits] [cfe-branch] r124624 - in /cfe/branches/Apple/sill/lib: Basic/SourceManager.cpp Lex/Lexer.cpp

Daniel Dunbar daniel at zuster.org
Mon Jan 31 15:39:34 PST 2011


Author: ddunbar
Date: Mon Jan 31 17:39:34 2011
New Revision: 124624

URL: http://llvm.org/viewvc/llvm-project?rev=124624&view=rev
Log:
Merge r124616:
--
Author: Douglas Gregor <dgregor at apple.com>
Date:   Mon Jan 31 22:42:36 2011 +0000

    Harden Lexer::GetBeginningOfToken() against bogus source locations and
    the disappearance/alteration of files.

Modified:
    cfe/branches/Apple/sill/lib/Basic/SourceManager.cpp
    cfe/branches/Apple/sill/lib/Lex/Lexer.cpp

Modified: cfe/branches/Apple/sill/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/sill/lib/Basic/SourceManager.cpp?rev=124624&r1=124623&r2=124624&view=diff
==============================================================================
--- cfe/branches/Apple/sill/lib/Basic/SourceManager.cpp (original)
+++ cfe/branches/Apple/sill/lib/Basic/SourceManager.cpp Mon Jan 31 17:39:34 2011
@@ -537,12 +537,21 @@
 
 llvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
   bool MyInvalid = false;
-  const llvm::MemoryBuffer *Buf = getBuffer(FID, &MyInvalid);
+  const SLocEntry &SLoc = getSLocEntry(FID.ID);
+  if (!SLoc.isFile()) {
+    if (Invalid) 
+      *Invalid = true;
+    return "<<<<<INVALID SOURCE LOCATION>>>>>";
+  }
+  
+  const llvm::MemoryBuffer *Buf
+    = SLoc.getFile().getContentCache()->getBuffer(Diag, *this, SourceLocation(), 
+                                                  &MyInvalid);
   if (Invalid)
     *Invalid = MyInvalid;
 
   if (MyInvalid)
-    return "";
+    return "<<<<<INVALID SOURCE LOCATION>>>>>";
   
   return Buf->getBuffer();
 }

Modified: cfe/branches/Apple/sill/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/sill/lib/Lex/Lexer.cpp?rev=124624&r1=124623&r2=124624&view=diff
==============================================================================
--- cfe/branches/Apple/sill/lib/Lex/Lexer.cpp (original)
+++ cfe/branches/Apple/sill/lib/Lex/Lexer.cpp Mon Jan 31 17:39:34 2011
@@ -254,6 +254,9 @@
                                           const SourceManager &SM,
                                           const LangOptions &LangOpts) {
   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
+  if (LocInfo.first.isInvalid())
+    return Loc;
+  
   bool Invalid = false;
   llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
   if (Invalid)
@@ -262,6 +265,9 @@
   // Back up from the current location until we hit the beginning of a line
   // (or the buffer). We'll relex from that point.
   const char *BufStart = Buffer.data();
+  if (LocInfo.second >= Buffer.size())
+    return Loc;
+  
   const char *StrData = BufStart+LocInfo.second;
   if (StrData[0] == '\n' || StrData[0] == '\r')
     return Loc;





More information about the llvm-branch-commits mailing list