[find-all-symbols] Fix racy yaml file writing.

If multiple find-all-symbols processes access the temporary directory
simultaneously with two files with the same name they would collide and
create a broken yaml file. Fix this by using the safe createUniqueFile
API from LLVM instead.

Differential Revision: http://reviews.llvm.org/D19717

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@268021 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2016-04-29 10:16:28 +00:00
Родитель 1d3304248a
Коммит 62b3188df0
3 изменённых файлов: 20 добавлений и 15 удалений

Просмотреть файл

@ -97,16 +97,11 @@ bool SymbolInfo::operator<(const SymbolInfo &Symbol) const {
std::tie(Symbol.Name, Symbol.FilePath, Symbol.LineNumber);
}
bool WriteSymboInfosToFile(llvm::StringRef FilePath,
const std::set<SymbolInfo> &Symbols) {
int FD = 0;
if (llvm::sys::fs::openFileForWrite(FilePath, FD, llvm::sys::fs::F_None))
return false;
llvm::raw_fd_ostream OS(FD, true);
bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
const std::set<SymbolInfo> &Symbols) {
llvm::yaml::Output yout(OS);
for (auto Symbol : Symbols)
yout << Symbol;
OS.close();
return true;
}

Просмотреть файл

@ -12,6 +12,7 @@
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include <set>
#include <string>
#include <vector>
@ -87,9 +88,9 @@ struct SymbolInfo {
bool operator<(const SymbolInfo &Symbol) const;
};
/// \brief Write SymbolInfos to a single file (YAML format).
bool WriteSymboInfosToFile(llvm::StringRef FilePath,
const std::set<SymbolInfo> &Symbols);
/// \brief Write SymbolInfos to a stream (YAML format).
bool WriteSymbolInfosToStream(llvm::raw_ostream &OS,
const std::set<SymbolInfo> &Symbols);
/// \brief Read SymbolInfos from a YAML document.
std::vector<SymbolInfo> ReadSymbolInfosFromYAML(llvm::StringRef Yaml);

Просмотреть файл

@ -59,10 +59,13 @@ public:
void Write(const std::string &Dir) {
for (const auto &Symbol : Symbols) {
SmallString<256> FilePath(Dir);
llvm::sys::path::append(
FilePath, llvm::sys::path::filename(Symbol.first) + ".yaml");
WriteSymboInfosToFile(FilePath, Symbol.second);
int FD;
SmallString<128> ResultPath;
llvm::sys::fs::createUniqueFile(
Dir + "/" + llvm::sys::path::filename(Symbol.first) + "-%%%%%%.yaml",
FD, ResultPath);
llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
WriteSymbolInfosToStream(OS, Symbol.second);
}
}
@ -90,7 +93,13 @@ bool Merge(llvm::StringRef MergeDir, llvm::StringRef OutputFile) {
UniqueSymbols.insert(Symbol);
}
WriteSymboInfosToFile(OutputFile, UniqueSymbols);
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_None);
if (EC) {
llvm::errs() << "Cann't open '" << OutputFile << "': " << EC.message()
<< '\n';
return false;
}
WriteSymbolInfosToStream(OS, UniqueSymbols);
return true;
}