Stop leaking file descriptors.

After the open+fstat optimization, files were already opened for FileManager::getBufferForFile() and we closed them after reading them.
The problem was that when -working-directory was passed, the code path that actually reuses & closes the already opened file descriptor
was not followed.

llvm-svn: 127639
This commit is contained in:
Argyrios Kyrtzidis
2011-03-15 00:47:44 +00:00
parent f6b01ff422
commit 669b0b1521

View File

@@ -466,21 +466,23 @@ llvm::MemoryBuffer *FileManager::
getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
llvm::OwningPtr<llvm::MemoryBuffer> Result;
llvm::error_code ec;
const char *Filename = Entry->getName();
// If the file is already open, use the open file descriptor.
if (Entry->FD != -1) {
ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
Entry->getSize());
if (ErrorStr)
*ErrorStr = ec.message();
close(Entry->FD);
Entry->FD = -1;
return Result.take();
}
// Otherwise, open the file.
if (FileSystemOpts.WorkingDir.empty()) {
const char *Filename = Entry->getName();
// If the file is already open, use the open file descriptor.
if (Entry->FD != -1) {
ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
Entry->getSize());
if (ErrorStr)
*ErrorStr = ec.message();
close(Entry->FD);
Entry->FD = -1;
return Result.take();
}
// Otherwise, open the file.
ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
if (ec && ErrorStr)
*ErrorStr = ec.message();