From owner-freebsd-bugs@FreeBSD.ORG Fri Jan 6 21:30:16 2012 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D5758106567A for ; Fri, 6 Jan 2012 21:30:16 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id B872C8FC18 for ; Fri, 6 Jan 2012 21:30:16 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.5/8.14.5) with ESMTP id q06LUGgV024241 for ; Fri, 6 Jan 2012 21:30:16 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.5/8.14.5/Submit) id q06LUGvF024236; Fri, 6 Jan 2012 21:30:16 GMT (envelope-from gnats) Date: Fri, 6 Jan 2012 21:30:16 GMT Message-Id: <201201062130.q06LUGvF024236@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: dfilter@FreeBSD.ORG (dfilter service) Cc: Subject: Re: bin/83359: commit references a PR X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: dfilter service List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Jan 2012 21:30:17 -0000 The following reply was made to PR bin/83359; it has been noted by GNATS. From: dfilter@FreeBSD.ORG (dfilter service) To: bug-followup@FreeBSD.org Cc: Subject: Re: bin/83359: commit references a PR Date: Fri, 6 Jan 2012 21:28:44 +0000 (UTC) Author: ghelmer Date: Fri Jan 6 21:28:29 2012 New Revision: 229735 URL: http://svn.freebsd.org/changeset/base/229735 Log: Handle memory allocation failures in devstat_getdevs(), devstat_selectdevs(), and devstat_buildmatch(). PR: bin/83359 Reviewed by: ken Modified: head/lib/libdevstat/devstat.c Modified: head/lib/libdevstat/devstat.c ============================================================================== --- head/lib/libdevstat/devstat.c Fri Jan 6 21:23:00 2012 (r229734) +++ head/lib/libdevstat/devstat.c Fri Jan 6 21:28:29 2012 (r229735) @@ -365,6 +365,12 @@ devstat_getdevs(kvm_t *kd, struct statin 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_selecti * 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_selecti 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_selecti || (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, stru 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)); _______________________________________________ svn-src-all@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"