[cfe-commits] r128139 - in /cfe/trunk: include/clang/Basic/SourceManager.h tools/libclang/CIndex.cpp

Ted Kremenek kremenek at apple.com
Tue Mar 22 19:16:44 PDT 2011


Author: kremenek
Date: Tue Mar 22 21:16:44 2011
New Revision: 128139

URL: http://llvm.org/viewvc/llvm-project?rev=128139&view=rev
Log:
Fix crash in clang_getInstantiationLoc() when SourceManager::getInstantiationLoc() can return a SourceLocatin with an invalid
FileID on invalid code.  Fixes <rdar://problem/9164623>.

Modified:
    cfe/trunk/include/clang/Basic/SourceManager.h
    cfe/trunk/tools/libclang/CIndex.cpp

Modified: cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceManager.h?rev=128139&r1=128138&r2=128139&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Tue Mar 22 21:16:44 2011
@@ -585,6 +585,12 @@
     return getSLocEntry(FID).getFile().getContentCache()->OrigEntry;
   }
 
+  /// Returns the FileEntry record for the provided SLocEntry.
+  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
+  {
+    return sloc.getFile().getContentCache()->OrigEntry;
+  }
+
   /// getBufferData - Return a StringRef to the source buffer data for the
   /// specified FileID.
   ///

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=128139&r1=128138&r2=128139&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Mar 22 21:16:44 2011
@@ -2718,7 +2718,22 @@
                            begin.int_data, end.int_data };
   return Result;
 }
+} // end: extern "C"
+
+static void createNullLocation(CXFile *file, unsigned *line,
+                               unsigned *column, unsigned *offset) {
+  if (file)
+   *file = 0;
+  if (line)
+   *line = 0;
+  if (column)
+   *column = 0;
+  if (offset)
+   *offset = 0;
+  return;
+}
 
+extern "C" {
 void clang_getInstantiationLocation(CXSourceLocation location,
                                     CXFile *file,
                                     unsigned *line,
@@ -2727,14 +2742,7 @@
   SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
 
   if (!location.ptr_data[0] || Loc.isInvalid()) {
-    if (file)
-      *file = 0;
-    if (line)
-      *line = 0;
-    if (column)
-      *column = 0;
-    if (offset)
-      *offset = 0;
+    createNullLocation(file, line, column, offset);
     return;
   }
 
@@ -2742,8 +2750,17 @@
     *static_cast<const SourceManager*>(location.ptr_data[0]);
   SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
 
+  // Check that the FileID is invalid on the instantiation location.
+  // This can manifest in invalid code.
+  FileID fileID = SM.getFileID(InstLoc);
+  const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID);
+  if (!sloc.isFile()) {
+    createNullLocation(file, line, column, offset);
+    return;
+  }
+
   if (file)
-    *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
+    *file = (void *)SM.getFileEntryForSLocEntry(sloc);
   if (line)
     *line = SM.getInstantiationLineNumber(InstLoc);
   if (column)





More information about the cfe-commits mailing list