[PATCH nvme-cli] nvme: fix some memory leak bugs

Ruozhu Li liruozhu at huawei.com
Thu Jul 15 23:10:40 PDT 2021


Signed-off-by: Ruozhu Li <liruozhu at huawei.com>
---
 nvme-print.c | 6 +++++-
 nvme-rpmb.c  | 5 ++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/nvme-print.c b/nvme-print.c
index 9444154..e808851 100755
--- a/nvme-print.c
+++ b/nvme-print.c
@@ -2618,6 +2618,8 @@ void nvme_show_relatives(const char *name)
 			comma = true;
 		}
 		fprintf(stderr, "\n\n");
+		for (i = 0; i < n; i++)
+			free(paths[i]);
 		free(paths);
 	}
 	free(path);
@@ -6382,8 +6384,10 @@ static void json_simple_ns(struct nvme_namespace *n, struct json_object *devices
 		return;
 
 	ret = stat(devnode, &st);
-	if (ret < 0)
+	if (ret < 0) {
+		free(devnode);
 		return;
+	}
 
 	device_attrs = json_create_object();
 	json_object_add_value_int(device_attrs, "NameSpace", n->nsid);
diff --git a/nvme-rpmb.c b/nvme-rpmb.c
index 0e11ef3..7dc0b58 100644
--- a/nvme-rpmb.c
+++ b/nvme-rpmb.c
@@ -278,7 +278,10 @@ static void rpmb_nonce_init(struct rpmb_data_frame_t *req)
 {
 	int num = rand();
 	unsigned char *hash = rpmb_md5((unsigned char *)&num, sizeof(num));
-	if (hash) memcpy(req->nonce, hash, sizeof(req->nonce));
+	if (hash) {
+		memcpy(req->nonce, hash, sizeof(req->nonce));
+		free(hash);
+	}
 }
 
 /* Read key from a given key buffer or key file */
-- 
2.25.1




More information about the Linux-nvme mailing list