Hi,

While using clang_indexSourceFile(), I found that it crashes faithfully
when you give it unsaved files. Upon further inspection I found this is
apparently caused by a double-deletion:

clang_indexSourceFile_Impl creates llvm::MemoryBuffers with the unsaved
file contents and uses MemBufferOwner to manage them.
However, it also creates an ASTUnit, which by default takes ownership of
the unsaved file buffers. This leads to double deletion of the unsaved file
buffers.

While it's possible to use ASTUnit::setOwnsRemappedFileBuffers(false) here,
that would mean that the unsaved file contents are no longer available to
the out_TU, which is presumably incorrect.

I've attached a (potential) fix.

Cheers,

-Olivier JG
Index: tools/libclang/Indexing.cpp
===================================================================
--- tools/libclang/Indexing.cpp	(revision 193241)
+++ tools/libclang/Indexing.cpp	(working copy)
@@ -487,16 +487,6 @@
   int result;
 };
 
-struct MemBufferOwner {
-  SmallVector<const llvm::MemoryBuffer *, 8> Buffers;
-  
-  ~MemBufferOwner() {
-    for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator
-           I = Buffers.begin(), E = Buffers.end(); I != E; ++I)
-      delete *I;
-  }
-};
-
 } // anonymous namespace
 
 static void clang_indexSourceFile_Impl(void *UserData) {
@@ -586,18 +576,11 @@
   if (CInvok->getFrontendOpts().Inputs.empty())
     return;
 
-  OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner());
-
-  // Recover resources if we crash before exiting this method.
-  llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner>
-    BufOwnerCleanup(BufOwner.get());
-
   for (unsigned I = 0; I != num_unsaved_files; ++I) {
     StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
     const llvm::MemoryBuffer *Buffer
       = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
     CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer);
-    BufOwner->Buffers.push_back(Buffer);
   }
 
   // Since libclang is primarily used by batch tools dealing with
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to