[sheepdog] [PATCH v1 2/2] dog: memory leak in vdi_setattr

Meng An isolate000 at gmail.com
Wed Jul 23 06:42:40 CEST 2014


From: meng an <isolate000 at gmail.com>

In function vdi_setattr, the pointer 'value' is allocated via
xmalloc, but its memory is not released when returns.

Signed-off-by: Meng An <anmeng.an at alibaba-inc.com>
---
 dog/vdi.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/dog/vdi.c b/dog/vdi.c
index 30739ff..a4eb91e 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -1126,7 +1126,8 @@ static int find_vdi_attr_oid(const char *vdiname, const char *tag, uint32_t snap
 
 static int vdi_setattr(int argc, char **argv)
 {
-	int ret, value_len = 0;
+	int value_len = 0;
+	int ret = EXIT_SUCCESS;
 	uint64_t attr_oid = 0;
 	uint32_t vid = 0, nr_copies = 0;
 	const char *vdiname = argv[optind++], *key;
@@ -1136,7 +1137,8 @@ static int vdi_setattr(int argc, char **argv)
 	key = argv[optind++];
 	if (!key) {
 		sd_err("Please specify the attribute key");
-		return EXIT_USAGE;
+		ret = EXIT_USAGE;
+		goto out;
 	}
 
 	value = argv[optind++];
@@ -1149,7 +1151,8 @@ reread:
 			   SD_MAX_VDI_ATTR_VALUE_LEN - offset);
 		if (ret < 0) {
 			sd_err("Failed to read attribute value from stdin: %m");
-			return EXIT_SYSFAIL;
+			ret = EXIT_SYSFAIL;
+			goto out;
 		}
 		if (ret > 0) {
 			offset += ret;
@@ -1168,19 +1171,27 @@ reread:
 	if (ret) {
 		if (ret == SD_RES_VDI_EXIST) {
 			sd_err("The attribute '%s' already exists", key);
-			return EXIT_EXISTS;
+			ret = EXIT_EXISTS;
+			goto out;
 		} else if (ret == SD_RES_NO_OBJ) {
 			sd_err("Attribute '%s' not found", key);
-			return EXIT_MISSING;
+			ret = EXIT_MISSING;
+			goto out;
 		} else if (ret == SD_RES_NO_VDI) {
 			sd_err("VDI not found");
-			return EXIT_MISSING;
+			ret = EXIT_MISSING;
+			goto out;
 		} else
 			sd_err("Failed to set attribute: %s", sd_strerror(ret));
-		return EXIT_FAILURE;
+		ret = EXIT_FAILURE;
+		goto out;
 	}
 
-	return EXIT_SUCCESS;
+out:
+	if (value)
+		free(value);
+
+	return ret;
 }
 
 static int vdi_getattr(int argc, char **argv)
-- 
1.7.1




More information about the sheepdog mailing list