From: Minwoo Im Date: Wed, 13 Mar 2019 05:08:38 +0000 (+0900) Subject: nvme: Close a fd leaked X-Git-Tag: v1.9~91^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=2b142c4cc0f8fa399c10974de5f30cab8f02b616;p=users%2Fsagi%2Fnvme-cli.git nvme: Close a fd leaked "output" file descriptor has not been closed not only when this command is successfully done, but error occurs in the middle of the procdure. This patch put "output" open after memory preparation so that we can add close_output label before free_mem. Signed-off-by: Minwoo Im --- diff --git a/nvme.c b/nvme.c index 5606bc50..3a821313 100644 --- a/nvme.c +++ b/nvme.c @@ -341,14 +341,6 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, struct goto close_fd; } - output = open(cfg.file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); - if (output < 0) { - fprintf(stderr, "Failed to open output file %s: %s!\n", - cfg.file_name, strerror(errno)); - err = output; - goto close_fd; - } - cfg.host_gen = !!cfg.host_gen; hdr = malloc(bs); page_log = malloc(bs); @@ -358,21 +350,28 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, struct err = ENOMEM; goto free_mem; } - memset(hdr, 0, bs); + + output = open(cfg.file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (output < 0) { + fprintf(stderr, "Failed to open output file %s: %s!\n", + cfg.file_name, strerror(errno)); + err = output; + goto free_mem; + } + err = nvme_get_telemetry_log(fd, hdr, cfg.host_gen, cfg.ctrl_init, bs, 0); if (err) { fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err), err); fprintf(stderr, "Failed to acquire telemetry header %d!\n", err); - close(output); - goto free_mem; + goto close_output; } err = write(output, (void *) hdr, bs); if (err != bs) { fprintf(stderr, "Failed to flush all data to file!"); - goto free_mem; + goto close_output; } switch (cfg.data_area) { @@ -388,7 +387,7 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, struct default: fprintf(stderr, "Invalid data area requested"); err = EINVAL; - goto free_mem; + goto close_output; } /* @@ -411,6 +410,9 @@ static int get_telemetry_log(int argc, char **argv, struct command *cmd, struct } offset += bs; } + + close_output: + close(output); free_mem: free(hdr); free(page_log);