]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-cli: fix memory leak in sec_send()
authorMinwoo Im <minwoo.im.dev@gmail.com>
Wed, 17 Jan 2018 11:18:29 +0000 (20:18 +0900)
committerMinwoo Im <minwoo.im.dev@gmail.com>
Wed, 17 Jan 2018 11:18:29 +0000 (20:18 +0900)
Fix memory leak for _sec_buf_.

==9303== HEAP SUMMARY:
==9303==     in use at exit: 0 bytes in 1 blocks
==9303==   total heap usage: 31 allocs, 30 frees, 3,915 bytes allocated
==9303==
==9303== Searching for pointers to 1 not-freed blocks
==9303== Checked 70,664 bytes
==9303==
==9303== 0 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9303==    at 0x4C2FFC6: memalign (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9303==    by 0x4C300D1: posix_memalign (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9303==    by 0x405A4C: sec_send (nvme.c:2415)
==9303==    by 0x419527: handle_plugin (plugin.c:150)
==9303==    by 0x401890: main (nvme.c:3812)
==9303==
==9303== LEAK SUMMARY:
==9303==    definitely lost: 0 bytes in 1 blocks
==9303==    indirectly lost: 0 bytes in 0 blocks
==9303==      possibly lost: 0 bytes in 0 blocks
==9303==    still reachable: 0 bytes in 0 blocks
==9303==         suppressed: 0 bytes in 0 blocks
==9303==
==9303== 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 891fe0f70d7eb1299844d5695d42bbbfdc3cb41e..459b3b73c0a132d2b4a65888a3c9358b23e44e80 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2420,7 +2420,8 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p
        if (read(sec_fd, sec_buf, sec_size) < 0) {
                fprintf(stderr, "Failed to read data from security file with %s\n",
                        strerror(errno));
-               return EINVAL;
+               err = EINVAL;
+               goto free;
        }
 
        err = nvme_sec_send(fd, cfg.namespace_id, cfg.nssf, cfg.spsp, cfg.secp,
@@ -2431,6 +2432,9 @@ static int sec_send(int argc, char **argv, struct command *cmd, struct plugin *p
                fprintf(stderr, "NVME Security Send Command Error:%d\n", err);
        else
                printf("NVME Security Send Command Success:%d\n", result);
+
+free:
+       free(sec_buf);
        return err;
 }