Set RetainRemappedFileBuffers to true

Reported by David Welch in #350.

This fixes double-free of llvm::MemoryBuffer when parsing fails.
This commit is contained in:
Fangrui Song 2019-03-29 07:48:58 -07:00
parent 539ca22e5e
commit 1d3fba276f
2 changed files with 2 additions and 4 deletions

View File

@ -1244,6 +1244,7 @@ Index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
if (!Clang->hasTarget())
return {};
Clang->getPreprocessorOpts().RetainRemappedFileBuffers = true;
#if LLVM_VERSION_MAJOR >= 9 // rC357037
Clang->createFileManager(FS);
#else
@ -1290,8 +1291,6 @@ Index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
LOG_S(ERROR) << "failed to index " << main;
return {};
}
for (auto &Buf : Bufs)
Buf.release();
std::vector<std::unique_ptr<IndexFile>> result;
for (auto &it : param.UID2File) {

View File

@ -301,6 +301,7 @@ std::unique_ptr<CompilerInstance> BuildCompilerInstance(
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
if (!Clang->hasTarget())
return nullptr;
Clang->getPreprocessorOpts().RetainRemappedFileBuffers = true;
// Construct SourceManager with UserFilesAreVolatile: true because otherwise
// RequiresNullTerminator: true may cause out-of-bounds read when a file is
// mmap'ed but is saved concurrently.
@ -482,7 +483,6 @@ void *CompletionMain(void *manager_) {
Clang->setCodeCompletionConsumer(task->Consumer.release());
if (!Parse(*Clang))
continue;
Buf.release();
task->on_complete(&Clang->getCodeCompletionConsumer());
}
@ -572,7 +572,6 @@ void *DiagnosticMain(void *manager_) {
continue;
if (!Parse(*Clang))
continue;
Buf.release();
auto Fill = [](const DiagBase &d, Diagnostic &ret) {
ret.range = lsRange{{d.range.start.line, d.range.start.column},