From: Clifford Wolf Date: Wed, 15 May 2019 11:51:02 +0000 (+0200) Subject: Do not leak file descriptors in cover.cc X-Git-Tag: yosys-0.9~121 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f67ec1b2350c3bd88edacba7ad799bf60b33da61;p=yosys.git Do not leak file descriptors in cover.cc Signed-off-by: Clifford Wolf --- diff --git a/passes/cmds/cover.cc b/passes/cmds/cover.cc index 721ebded4..1128116b4 100644 --- a/passes/cmds/cover.cc +++ b/passes/cmds/cover.cc @@ -98,22 +98,23 @@ struct CoverPass : public Pass { } if ((args[argidx] == "-o" || args[argidx] == "-a" || args[argidx] == "-d") && argidx+1 < args.size()) { 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") { #ifdef _WIN32 log_cmd_error("The 'cover -d' option is not supported on win32.\n"); #else char filename_buffer[4096]; snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", filename.c_str(), getpid()); - mkstemps(filename_buffer, 4); - filename = filename_buffer; + f = fdopen(mkstemps(filename_buffer, 4), "w"); #endif + } else { + f = fopen(filename.c_str(), open_mode); } - FILE *f = fopen(filename.c_str(), open_mode); if (f == NULL) { for (auto f : out_files) 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); continue;