[Ocfs2-tools-devel] [PATCH] ocfs2_hb_ctl: Fix memory leak problems in main

piaojun piaojun at huawei.com
Fri May 27 21:44:12 PDT 2016


In main, hbo.dev_str, hbo.service and hbo.uuid_str should be freed after
use.

Signed-off-by: Jun Piao <piaojun at huawei.com>
---
 ocfs2_hb_ctl/ocfs2_hb_ctl.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/ocfs2_hb_ctl/ocfs2_hb_ctl.c b/ocfs2_hb_ctl/ocfs2_hb_ctl.c
index e4daa92..4a9ffbf 100644
--- a/ocfs2_hb_ctl/ocfs2_hb_ctl.c
+++ b/ocfs2_hb_ctl/ocfs2_hb_ctl.c
@@ -171,13 +171,13 @@ out:
 	return err;
 }
 
-static errcode_t get_uuid(char *dev, char *uuid)
+static errcode_t get_uuid(char *dev, char **uuid)
 {
 	errcode_t ret;
 
 	ret = get_desc(dev);
 	if (!ret) 
-		strcpy(uuid, region_desc->r_name);
+		*uuid = strdup(region_desc->r_name);
 
 	return ret;
 }
@@ -543,7 +543,7 @@ int main(int argc, char **argv)
 	struct hb_ctl_options hbo = {
 		.action = HB_ACTION_UNKNOWN,
 	};
-	char hbuuid[33];
+	char *hbuuid = NULL;
 
 	setbuf(stdout, NULL);
 	setbuf(stderr, NULL);
@@ -577,7 +577,7 @@ int main(int argc, char **argv)
 	}
 
 	if (!hbo.uuid_str) {
-		err = get_uuid(hbo.dev_str, hbuuid);
+		err = get_uuid(hbo.dev_str, &hbuuid);
 		if (err) {
 			com_err(progname, err, "while reading uuid");
 			ret = -EINVAL;
@@ -631,6 +631,9 @@ int main(int argc, char **argv)
 	block_signals(SIG_UNBLOCK);
 
 bail:
+	ocfs2_free(&hbo.dev_str);
+	ocfs2_free(&hbo.service);
+	ocfs2_free(&hbo.uuid_str);
 	free_desc();
 	return ret ? 1 : 0;
 }
-- 
1.8.4.3




More information about the Ocfs2-tools-devel mailing list