Made file opening more safe

This commit is contained in:
AdvaySingh1 2026-04-10 13:10:26 -07:00
parent 03554f0d58
commit cf78adc729
1 changed files with 4 additions and 6 deletions

View File

@ -17,6 +17,7 @@
*
*/
#include <memory>
#include "kernel/yosys.h"
#include "kernel/sigtools.h"
@ -249,9 +250,9 @@ struct ReportFanoutPass : public Pass {
log_cmd_error("Missing required -limit option (must be a non-negative integer).\n");
// Open output file if requested
FILE *f = nullptr;
std::unique_ptr<FILE, decltype(&fclose)> f(nullptr, &fclose);
if (!filename.empty()) {
f = fopen(filename.c_str(), "w");
f.reset(fopen(filename.c_str(), "w"));
if (!f)
log_cmd_error("Cannot open file '%s' for writing.\n", filename.c_str());
}
@ -261,11 +262,8 @@ struct ReportFanoutPass : public Pass {
{
log("Analyzing fanout for module %s...\n", log_id(module));
ReportFanoutWorker worker(module, skip_clk_rst);
worker.report(limit, f);
worker.report(limit, f.get());
}
if (f)
fclose(f);
}
} ReportFanoutPass;