]> www.infradead.org Git - users/hch/nvme-cli.git/commitdiff
nvme-cli: fix memory leak in dir_send()
authorMinwoo Im <minwoo.im.dev@gmail.com>
Wed, 17 Jan 2018 11:25:14 +0000 (20:25 +0900)
committerMinwoo Im <minwoo.im.dev@gmail.com>
Wed, 17 Jan 2018 11:25:14 +0000 (20:25 +0900)
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 <minwoo.im.dev@gmail.com>
nvme.c

diff --git a/nvme.c b/nvme.c
index 459b3b73c0a132d2b4a65888a3c9358b23e44e80..4b3073e749846d85c2c32cfc1e73f52ca1d89796 100644 (file)
--- 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;