From: Minwoo Im Date: Wed, 17 Jan 2018 11:25:14 +0000 (+0900) Subject: nvme-cli: fix memory leak in dir_send() X-Git-Tag: v1.6~113^2~3 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=cb7ea5624fc72bb5147128bf530597c3b35f1cc0;p=users%2Fhch%2Fnvme-cli.git nvme-cli: fix memory leak in dir_send() Fix memory leak for _buf_. ==9566== HEAP SUMMARY: ==9566== in use at exit: 1,024 bytes in 1 blocks ==9566== total heap usage: 52 allocs, 51 frees, 16,681 bytes allocated ==9566== ==9566== Searching for pointers to 1 not-freed blocks ==9566== Checked 70,656 bytes ==9566== ==9566== 1,024 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==9566== at 0x4C2FFC6: memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==9566== by 0x4C300D1: posix_memalign (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==9566== by 0x402174: dir_send (nvme.c:2535) ==9566== by 0x419317: handle_plugin (plugin.c:150) ==9566== by 0x401890: main (nvme.c:3818) ==9566== ==9566== LEAK SUMMARY: ==9566== definitely lost: 1,024 bytes in 1 blocks ==9566== indirectly lost: 0 bytes in 0 blocks ==9566== possibly lost: 0 bytes in 0 blocks ==9566== still reachable: 0 bytes in 0 blocks ==9566== suppressed: 0 bytes in 0 blocks ==9566== ==9566== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) Signed-off-by: Minwoo Im --- diff --git a/nvme.c b/nvme.c index 459b3b7..4b3073e 100644 --- a/nvme.c +++ b/nvme.c @@ -2541,12 +2541,14 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p ffd = open(cfg.file, O_RDONLY); if (ffd <= 0) { fprintf(stderr, "no firmware file provided\n"); - return -EINVAL; + err = EINVAL; + goto free; } } if (read(ffd, (void *)buf, cfg.data_len) < 0) { fprintf(stderr, "failed to read data buffer from input file\n"); - return EINVAL; + err = EINVAL; + goto free; } } @@ -2554,7 +2556,7 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p cfg.data_len, dw12, buf, &result); if (err < 0) { perror("dir-send"); - return errno; + goto free; } if (!err) { printf("dir-send: type %#x, operation %#x, spec_val %#x, nsid %#x, result %#x \n", @@ -2569,6 +2571,8 @@ static int dir_send(int argc, char **argv, struct command *cmd, struct plugin *p else if (err > 0) fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(err), err); + +free: if (buf) free(buf); return err;