summaryrefslogtreecommitdiffstats
path: root/lib/libdevstat
diff options
context:
space:
mode:
authorghelmer <ghelmer@FreeBSD.org>2012-01-06 21:28:29 +0000
committerghelmer <ghelmer@FreeBSD.org>2012-01-06 21:28:29 +0000
commit04dcea5a10395c197bfdd4726e912758cc2714b3 (patch)
tree1ac4462c9bd2d6e2dc5763e415b9f62c487047e8 /lib/libdevstat
parent3a9031f3c366078351d7d77f6885a81b881d04f0 (diff)
downloadFreeBSD-src-04dcea5a10395c197bfdd4726e912758cc2714b3.zip
FreeBSD-src-04dcea5a10395c197bfdd4726e912758cc2714b3.tar.gz
Handle memory allocation failures in devstat_getdevs(), devstat_selectdevs(),
and devstat_buildmatch(). PR: bin/83359 Reviewed by: ken
Diffstat (limited to 'lib/libdevstat')
-rw-r--r--lib/libdevstat/devstat.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/lib/libdevstat/devstat.c b/lib/libdevstat/devstat.c
index 06d2148..ff0767a 100644
--- a/lib/libdevstat/devstat.c
+++ b/lib/libdevstat/devstat.c
@@ -365,6 +365,12 @@ devstat_getdevs(kvm_t *kd, struct statinfo *stats)
dssize = (dinfo->numdevs * sizeof(struct devstat)) +
sizeof(long);
dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
+ if (dinfo->mem_ptr == NULL) {
+ snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+ "%s: Cannot allocate memory for mem_ptr element",
+ __func__);
+ return(-1);
+ }
} else
dssize = (dinfo->numdevs * sizeof(struct devstat)) +
sizeof(long);
@@ -567,7 +573,7 @@ devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
* either enlarge or reduce the size of the device selection list.
*/
} else if (*num_selections != numdevs) {
- *dev_select = (struct device_selection *)realloc(*dev_select,
+ *dev_select = (struct device_selection *)reallocf(*dev_select,
numdevs * sizeof(struct device_selection));
*select_generation = current_generation;
init_selections = 1;
@@ -581,6 +587,13 @@ devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
init_selections = 1;
}
+ if (*dev_select == NULL) {
+ snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+ "%s: Cannot (re)allocate memory for dev_select argument",
+ __func__);
+ return(-1);
+ }
+
/*
* If we're in "only" mode, we want to clear out the selected
* variable since we're going to select exactly what the user wants
@@ -608,6 +621,12 @@ devstat_selectdevs(struct device_selection **dev_select, int *num_selected,
|| (perf_select != 0)) && (changed == 0)){
old_dev_select = (struct device_selection *)malloc(
*num_selections * sizeof(struct device_selection));
+ if (old_dev_select == NULL) {
+ snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+ "%s: Cannot allocate memory for selection list backup",
+ __func__);
+ return(-1);
+ }
old_num_selections = *num_selections;
bcopy(*dev_select, old_dev_select,
sizeof(struct device_selection) * *num_selections);
@@ -1028,16 +1047,17 @@ devstat_buildmatch(char *match_str, struct devstat_match **matches,
return(-1);
}
- /*
- * Since you can't realloc a pointer that hasn't been malloced
- * first, we malloc first and then realloc.
- */
if (*num_matches == 0)
- *matches = (struct devstat_match *)malloc(
- sizeof(struct devstat_match));
- else
- *matches = (struct devstat_match *)realloc(*matches,
- sizeof(struct devstat_match) * (*num_matches + 1));
+ *matches = NULL;
+
+ *matches = (struct devstat_match *)reallocf(*matches,
+ sizeof(struct devstat_match) * (*num_matches + 1));
+
+ if (*matches == NULL) {
+ snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+ "%s: Cannot allocate memory for matches list", __func__);
+ return(-1);
+ }
/* Make sure the current entry is clear */
bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
OpenPOWER on IntegriCloud