Module Name: src
Committed By: christos
Date: Wed Mar 9 19:53:32 UTC 2016
Modified Files:
src/sbin/raidctl: rf_configure.c
Log Message:
PR/50921: David Binderman: Fix memory leak
To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sbin/raidctl/rf_configure.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/raidctl/rf_configure.c
diff -u src/sbin/raidctl/rf_configure.c:1.25 src/sbin/raidctl/rf_configure.c:1.26
--- src/sbin/raidctl/rf_configure.c:1.25 Wed Jan 27 13:34:02 2010
+++ src/sbin/raidctl/rf_configure.c Wed Mar 9 14:53:32 2016
@@ -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 @@ rf_ReadSpareTable(RF_SparetWait_t *req,
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 @@ rf_ReadSpareTable(RF_SparetWait_t *req,
/* 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 @@ rf_ReadSpareTable(RF_SparetWait_t *req,
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 @@ rf_ReadSpareTable(RF_SparetWait_t *req,
fclose(fp);
return ((void *) table);
+out:
+ if (fp)
+ fclose(fp);
+ for (i = 0; i < req->TablesPerSpareRegion; i++)
+ free(table[i]);
+ free(table);
+ return NULL;
}