Module Name:    src
Committed By:   christos
Date:           Thu Dec  3 21:40:32 UTC 2015

Modified Files:
        src/sbin/gpt: backup.c

Log Message:
CID 1341558: Fix proplib memory leaks


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sbin/gpt/backup.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/gpt/backup.c
diff -u src/sbin/gpt/backup.c:1.15 src/sbin/gpt/backup.c:1.16
--- src/sbin/gpt/backup.c:1.15	Wed Dec  2 23:39:41 2015
+++ src/sbin/gpt/backup.c	Thu Dec  3 16:40:32 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: backup.c,v 1.15 2015/12/03 04:39:41 christos Exp $");
+__RCSID("$NetBSD: backup.c,v 1.16 2015/12/03 21:40:32 christos Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -66,10 +66,7 @@ struct gpt_cmd c_backup = {
 
 #define usage() gpt_usage(NULL, &c_backup)
 
-#define PROP_ERR(x)	if (!(x)) {			\
-		gpt_warnx(gpt, "proplib failure");	\
-		return -1;				\
-	}
+#define PROP_ERR(x)	if (!(x)) goto cleanup
 
 #define prop_uint(a) prop_number_create_unsigned_integer(a)
 
@@ -145,6 +142,11 @@ store_mbr(gpt_t gpt, unsigned int i, con
 	rc = prop_array_add(*mbr_array, mbr_dict);
 	PROP_ERR(rc);
 	return 0;
+cleanup:
+	if (mbr_dict)
+		prop_object_release(mbr_dict);
+	gpt_warnx(gpt, "proplib failure");
+	return -1;
 }
 
 static int
@@ -171,6 +173,10 @@ store_gpt(gpt_t gpt, const struct gpt_hd
 	rc = prop_dictionary_set(*type_dict, "entries", propnum);
 	PROP_ERR(rc);
 	return 0;
+cleanup:
+	if (*type_dict)
+		prop_object_release(*type_dict);
+	return -1;
 }
 
 static int
@@ -186,12 +192,15 @@ store_tbl(gpt_t gpt, const map_t m, prop
 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
 	bool rc;
 
+	*type_dict = NULL;
+
+	gpt_array = prop_array_create();
+	PROP_ERR(gpt_array);
+
 	*type_dict = prop_dictionary_create();
 	PROP_ERR(*type_dict);
 
 	ent = m->map_data;
-	gpt_array = prop_array_create();
-	PROP_ERR(gpt_array);
 	for (i = 1, ent = m->map_data;
 	    (const char *)ent < (const char *)(m->map_data) +
 	    m->map_size * gpt->secsz; i++, ent++) {
@@ -236,6 +245,12 @@ store_tbl(gpt_t gpt, const map_t m, prop
 	PROP_ERR(rc);
 	prop_object_release(gpt_array);
 	return 0;
+cleanup:
+	if (*type_dict)
+		prop_object_release(*type_dict);
+	if (gpt_array)
+		prop_object_release(gpt_array);
+	return -1;
 }
 
 static int
@@ -274,7 +289,7 @@ backup(gpt_t gpt, const char *outfile)
 			mbr_array = NULL;
 			for (i = 0; i < 4; i++) {
 				if (store_mbr(gpt, i, mbr, &mbr_array) == -1)
-					return -1;
+					goto cleanup;
 			}
 			if (mbr_array != NULL) {
 				rc = prop_dictionary_set(type_dict,
@@ -288,7 +303,7 @@ backup(gpt_t gpt, const char *outfile)
 			break;
 		case MAP_TYPE_PRI_GPT_HDR:
 			if (store_gpt(gpt, m->map_data, &type_dict) == -1)
-				return -1;
+				goto cleanup;
 
 			rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
 			PROP_ERR(rc);
@@ -296,7 +311,7 @@ backup(gpt_t gpt, const char *outfile)
 			break;
 		case MAP_TYPE_PRI_GPT_TBL:
 			if (store_tbl(gpt, m, &type_dict) == -1)
-				return -1;
+				goto cleanup;
 			rc = prop_dictionary_set(props, "GPT_TBL", type_dict);
 			PROP_ERR(rc);
 			prop_object_release(type_dict);
@@ -310,13 +325,18 @@ backup(gpt_t gpt, const char *outfile)
 	fp = strcmp(outfile, "-") == 0 ? stdout : fopen(outfile, "w");
 	if (fp == NULL) {
 		gpt_warn(gpt, "Can't open `%s'", outfile);
-		return -1;
+		free(propext);
+		goto cleanup;
 	}
 	fputs(propext, fp);
 	if (fp != stdout)
 		fclose(fp);
 	free(propext);
 	return 0;
+cleanup:
+	if (props)
+		prop_object_release(props);
+	return -1;
 }
 
 static int

Reply via email to