[clang-tools-extra] r268021 - [find-all-symbols] Fix racy yaml file writing.
Benjamin Kramer via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 29 03:16:28 PDT 2016
Author: d0k
Date: Fri Apr 29 05:16:28 2016
New Revision: 268021
URL: http://llvm.org/viewvc/llvm-project?rev=268021&view=rev
Log:
[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
Modified:
clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp
clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h
clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp?rev=268021&r1=268020&r2=268021&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.cpp Fri Apr 29 05:16:28 2016
@@ -97,16 +97,11 @@ bool SymbolInfo::operator<(const SymbolI
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;
}
Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h?rev=268021&r1=268020&r2=268021&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h (original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/SymbolInfo.h Fri Apr 29 05:16:28 2016
@@ -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);
Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp?rev=268021&r1=268020&r2=268021&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp Fri Apr 29 05:16:28 2016
@@ -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, llv
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;
}
More information about the cfe-commits
mailing list