Source-Changes-HG archive

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

[src/trunk]: src/sbin/raidctl PR/50921: David Binderman: Fix memory leak



details:   https://anonhg.NetBSD.org/src/rev/20605c60fc89
branches:  trunk
changeset: 344012:20605c60fc89
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Mar 09 19:53:32 2016 +0000

description:
PR/50921: David Binderman: Fix memory leak

diffstat:

 sbin/raidctl/rf_configure.c |  48 ++++++++++++++++++++++++--------------------
 1 files changed, 26 insertions(+), 22 deletions(-)

diffs (101 lines):

diff -r f7d51f817d2e -r 20605c60fc89 sbin/raidctl/rf_configure.c
--- a/sbin/raidctl/rf_configure.c       Wed Mar 09 19:48:24 2016 +0000
+++ b/sbin/raidctl/rf_configure.c       Wed Mar 09 19:53:32 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rf_configure.c,v 1.25 2010/01/27 18:34:02 christos Exp $       */
+/*     $NetBSD: rf_configure.c,v 1.26 2016/03/09 19:53:32 christos Exp $       */
 
 /*
  * Copyright (c) 1995 Carnegie-Mellon University.
@@ -49,7 +49,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: rf_configure.c,v 1.25 2010/01/27 18:34:02 christos Exp $");
+__RCSID("$NetBSD: rf_configure.c,v 1.26 2016/03/09 19:53:32 christos Exp $");
 #endif
 
 
@@ -495,21 +495,19 @@
            spareDisk, spareBlkOffset;
        char buf[1024], targString[100], errString[100];
        RF_SpareTableEntry_t **table;
-       FILE *fp;
+       FILE *fp = NULL;
 
        /* allocate and initialize the table */
-       table = malloc(req->TablesPerSpareRegion * 
-                      sizeof(RF_SpareTableEntry_t *));
+       table = calloc(req->TablesPerSpareRegion, sizeof(*table));
        if (table == NULL) {
-               warnx("rf_ReadSpareTable: Unable to allocate table");
-               return (NULL);
+               warn("%s: Unable to allocate table", __func__);
+               return NULL;
        }
        for (i = 0; i < req->TablesPerSpareRegion; i++) {
-               table[i] = malloc(req->BlocksPerTable * 
-                                 sizeof(RF_SpareTableEntry_t));
+               table[i] = calloc(req->BlocksPerTable, sizeof(**table));
                if (table[i] == NULL) {
-                       warnx("rf_ReadSpareTable: Unable to allocate table");
-                       return (NULL);  /* XXX should cleanup too! */
+                       warn("%s: Unable to allocate table", __func__);
+                       goto out;
                }
                for (j = 0; j < req->BlocksPerTable; j++)
                        table[i][j].spareDisk =
@@ -518,22 +516,22 @@
 
        /* 2.  open sparemap file, sanity check */
        if ((fp = fopen(fname, "r")) == NULL) {
-               warn("rf_ReadSpareTable: Can't open sparemap file %s", fname);
-               return (NULL);
+               warn("%s: Can't open sparemap file %s", __func__, fname);
+               goto out;
        }
        if (rf_get_next_nonblank_line(buf, 1024, fp,
-           "Invalid sparemap file:  can't find header line\n")) {
-               fclose(fp);
-               return (NULL);
-       }
-       if (buf[strlen(buf) - 1] == '\n')
-               buf[strlen(buf) - 1] = '\0';
+           "Invalid sparemap file:  can't find header line\n"))
+               goto out;
+
+       size_t len = strlen(buf);
+       if (len != 0 && buf[len - 1] == '\n')
+               buf[len - 1] = '\0';
 
        snprintf(targString, sizeof(targString), "fdisk %d\n", req->fcol);
        snprintf(errString, sizeof(errString),
            "Invalid sparemap file:  can't find \"fdisk %d\" line\n",
            req->fcol);
-       while (1) {
+       for (;;) {
                rf_get_next_nonblank_line(buf, 1024, fp, errString);
                if (!strncmp(buf, targString, strlen(targString)))
                        break;
@@ -547,8 +545,7 @@
                if (numFound != 4) {
                        warnx("Sparemap file prematurely exhausted after %d "
                            "of %d lines", i, linecount);
-                       fclose(fp);
-                       return (NULL);
+                       goto out;
                }
 
                table[tableNum][tupleNum].spareDisk = spareDisk;
@@ -558,4 +555,11 @@
 
        fclose(fp);
        return ((void *) table);
+out:
+       if (fp)
+               fclose(fp);
+       for (i = 0; i < req->TablesPerSpareRegion; i++)
+               free(table[i]);
+       free(table);
+       return NULL;
 }



Home | Main Index | Thread Index | Old Index