Do not leak file descriptors in cover.cc
authorClifford Wolf <clifford@clifford.at>
Wed, 15 May 2019 11:51:02 +0000 (13:51 +0200)
committerClifford Wolf <clifford@clifford.at>
Wed, 15 May 2019 11:51:02 +0000 (13:51 +0200)
Signed-off-by: Clifford Wolf <clifford@clifford.at>
passes/cmds/cover.cc

index 721ebded4d1b74186c6e69dba079f624566eb53b..1128116b4f24f273a79612d7d46df7124e4ba8bb 100644 (file)
@@ -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;