Do not leak file descriptors in cover.cc

Signed-off-by: Clifford Wolf <clifford@clifford.at>
This commit is contained in:
Clifford Wolf 2019-05-15 13:51:02 +02:00
parent 4fd0e11214
commit f67ec1b235
1 changed files with 6 additions and 5 deletions

View File

@ -98,22 +98,23 @@ struct CoverPass : public Pass {
} }
if ((args[argidx] == "-o" || args[argidx] == "-a" || args[argidx] == "-d") && argidx+1 < args.size()) { if ((args[argidx] == "-o" || args[argidx] == "-a" || args[argidx] == "-d") && argidx+1 < args.size()) {
const char *open_mode = args[argidx] == "-a" ? "a+" : "w"; const char *open_mode = args[argidx] == "-a" ? "a+" : "w";
std::string filename = args[++argidx]; const std::string &filename = args[++argidx];
FILE *f = nullptr;
if (args[argidx-1] == "-d") { if (args[argidx-1] == "-d") {
#ifdef _WIN32 #ifdef _WIN32
log_cmd_error("The 'cover -d' option is not supported on win32.\n"); log_cmd_error("The 'cover -d' option is not supported on win32.\n");
#else #else
char filename_buffer[4096]; char filename_buffer[4096];
snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", filename.c_str(), getpid()); snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", filename.c_str(), getpid());
mkstemps(filename_buffer, 4); f = fdopen(mkstemps(filename_buffer, 4), "w");
filename = filename_buffer;
#endif #endif
} else {
f = fopen(filename.c_str(), open_mode);
} }
FILE *f = fopen(filename.c_str(), open_mode);
if (f == NULL) { if (f == NULL) {
for (auto f : out_files) for (auto f : out_files)
fclose(f); fclose(f);
log_cmd_error("Can't create file %s.\n", args[argidx].c_str()); log_cmd_error("Can't create file %s%s.\n", args[argidx-1] == "-d" ? "in directory " : "", args[argidx].c_str());
} }
out_files.push_back(f); out_files.push_back(f);
continue; continue;