From 0d7702e5e6a2b6364603995555bd68cb788f3046 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Mon, 1 Apr 2013 12:24:24 +0200 Subject: [PATCH] Delete temp file after usage --- src/Buffer.cpp | 18 ++++++++++-------- src/support/FileName.cpp | 5 +++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 9103abda0b..5a834cfe2f 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -988,22 +988,24 @@ bool Buffer::readString(string const & s) FileName const fn = FileName::tempName("Buffer_readString"); int file_format; - ReadStatus const ret_plf = parseLyXFormat(lex, fn, file_format); - if (ret_plf != ReadSuccess) - return ret_plf; + bool success = parseLyXFormat(lex, fn, file_format) == ReadSuccess; - if (file_format != LYX_FORMAT) { + if (success && file_format != LYX_FORMAT) { // We need to call lyx2lyx, so write the input to a file ofstream os(fn.toFilesystemEncoding().c_str()); os << s; os.close(); // lyxvc in readFile - return readFile(fn) == ReadSuccess; + if (readFile(fn) != ReadSuccess) + success = false; } - if (readDocument(lex)) - return false; - return true; + if (success) + if (readDocument(lex)) + success = false; + if (fn.exists()) + fn.removeFile(); + return success; } diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 96317bed27..3cc18bc9ae 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -437,6 +437,11 @@ FileNameList FileName::dirList(string const & ext) const static string createTempFile(QString const & mask) { + // FIXME: This is not safe. QTemporaryFile creates a file in open(), + // but the file is deleted when qt_tmp goes out of scope. + // Therefore the next call to createTempFile() may create the + // same file again. To make this safe the QTemporaryFile object + // needs to be kept for the whole life time of the temp file name. QTemporaryFile qt_tmp(mask); if (qt_tmp.open()) { string const temp_file = fromqstr(qt_tmp.fileName()); -- 2.39.2