Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/gpt CID 1341558: Fix proplib memory leaks



details:   https://anonhg.NetBSD.org/src/rev/fb850afbbc74
branches:  trunk
changeset: 812186:fb850afbbc74
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Dec 03 21:40:32 2015 +0000

description:
CID 1341558: Fix proplib memory leaks

diffstat:

 sbin/gpt/backup.c |  42 +++++++++++++++++++++++++++++++-----------
 1 files changed, 31 insertions(+), 11 deletions(-)

diffs (125 lines):

diff -r 9441d8b4d756 -r fb850afbbc74 sbin/gpt/backup.c
--- a/sbin/gpt/backup.c Thu Dec 03 21:30:54 2015 +0000
+++ b/sbin/gpt/backup.c Thu Dec 03 21:40:32 2015 +0000
@@ -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 @@
 
 #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 @@
        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 @@
        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 @@
        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 @@
        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 @@
                        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 @@
                        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 @@
                        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 @@
        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



Home | Main Index | Thread Index | Old Index