From 7531a102b45fbddf7ad63cc8a2deb7cc27419307 Mon Sep 17 00:00:00 2001 From: iarspider Date: Tue, 5 Mar 2024 12:35:18 +0100 Subject: [PATCH] edmWriteConfigs: don't use unsecure tmpnam(...) --- FWCore/ParameterSet/bin/edmWriteConfigs.cpp | 37 ++++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/FWCore/ParameterSet/bin/edmWriteConfigs.cpp b/FWCore/ParameterSet/bin/edmWriteConfigs.cpp index b056ed9074eb1..c767fbe0520ec 100644 --- a/FWCore/ParameterSet/bin/edmWriteConfigs.cpp +++ b/FWCore/ParameterSet/bin/edmWriteConfigs.cpp @@ -54,6 +54,8 @@ #include #include #include +#include +#include static char const* const kHelpOpt = "help"; static char const* const kHelpCommandOpt = "help,h"; @@ -136,18 +138,35 @@ namespace { } } + bool open_temp(std::string& path, std::ofstream& f) { + path += "/XXXXXX"; + std::vector dst_path(path.begin(), path.end()); + dst_path.push_back('\0'); + + int fd = mkstemp(&dst_path[0]); + if(fd != -1) { + path.assign(dst_path.begin(), dst_path.end() - 1); + f.open(path.c_str(), + std::ios_base::trunc | std::ios_base::out); + close(fd); + return true; + } + return false; + } + void writeModulesFile() { if (std::filesystem::exists(std::filesystem::current_path() / "modules.py")) return; - std::array buffer; - std::tmpnam(buffer.data()); - std::ofstream file{buffer.data()}; - - file << "from FWCore.ParameterSet.ModulesProxy import _setupProxies\n" - "locals().update(_setupProxies(__file__))\n"; - file.close(); - std::filesystem::copy(buffer.data(), "modules.py"); - std::filesystem::remove(buffer.data()); + std::ofstream file; + std::string path(std::filesystem::current_path()); + bool res = open_temp(path, file); + if (res) { + file << "from FWCore.ParameterSet.ModulesProxy import _setupProxies\n" + "locals().update(_setupProxies(__file__))\n"; + file.close(); + std::filesystem::copy(buffer.data(), "modules.py"); + std::filesystem::remove(buffer.data()); + } } } // namespace