Skip to content

Commit

Permalink
edmWriteConfigs: don't use unsecure tmpnam(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
iarspider authored Mar 5, 2024
1 parent d94472d commit 7531a10
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions FWCore/ParameterSet/bin/edmWriteConfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include <sstream>
#include <fstream>
#include <filesystem>
#include <stdlib.h>
#include <unistd.h>

static char const* const kHelpOpt = "help";
static char const* const kHelpCommandOpt = "help,h";
Expand Down Expand Up @@ -136,18 +138,35 @@ namespace {
}
}

bool open_temp(std::string& path, std::ofstream& f) {
path += "/XXXXXX";
std::vector<char> 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<char, L_tmpnam> 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

Expand Down

0 comments on commit 7531a10

Please sign in to comment.