Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 22 Apr 2017 16:02:40 +0000 (UTC)
From:      "Pedro F. Giffuni" <pfg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r317284 - head/usr.sbin/pmcstat
Message-ID:  <201704221602.v3MG2eC3065978@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pfg
Date: Sat Apr 22 16:02:40 2017
New Revision: 317284
URL: https://svnweb.freebsd.org/changeset/base/317284

Log:
  pmcstat(8); unsign some allocation variables and use reallocarray(3).
  
  Use unsigned values in some internal variables that will be used during
  allocation. The variables are used in reduced scope and have no chance of
  becoming negative.
  
  Provide bounds checking through reallocarray(3).
  
  MFC after:	2 weeks

Modified:
  head/usr.sbin/pmcstat/pmcpl_calltree.c
  head/usr.sbin/pmcstat/pmcstat_log.c

Modified: head/usr.sbin/pmcstat/pmcpl_calltree.c
==============================================================================
--- head/usr.sbin/pmcstat/pmcpl_calltree.c	Sat Apr 22 14:50:11 2017	(r317283)
+++ head/usr.sbin/pmcstat/pmcpl_calltree.c	Sat Apr 22 16:02:40 2017	(r317284)
@@ -185,7 +185,7 @@ pmcpl_ct_samples_free(struct pmcpl_ct_sa
 static void
 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
 {
-	int npmcs;
+	unsigned int npmcs;
 
 	/* Enough storage. */
 	if (pmcstat_npmcs <= samples->npmcs)
@@ -193,7 +193,7 @@ pmcpl_ct_samples_grow(struct pmcpl_ct_sa
 
 	npmcs = samples->npmcs +
 	    max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
-	samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
+	samples->sb = reallocarray(samples->sb, npmcs, sizeof(unsigned));
 	if (samples->sb == NULL)
 		errx(EX_SOFTWARE, "ERROR: out of memory");
 	bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
@@ -226,13 +226,13 @@ pmcpl_ct_samples_root(struct pmcpl_ct_sa
 static void
 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
 {
-	int nmaxsize;
+	unsigned int nmaxsize;
 
 	if (cursize < *maxsize)
 		return;
 
 	nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
-	*items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
+	*items = reallocarray(*items, nmaxsize, sizeof(struct pmcpl_ct_arc));
 	if (*items == NULL)
 		errx(EX_SOFTWARE, "ERROR: out of memory");
 	bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
@@ -247,13 +247,13 @@ pmcpl_ct_arc_grow(int cursize, int *maxs
 static void
 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
 {
-	int nmaxsize;
+	unsigned int nmaxsize;
 
 	if (cursize < *maxsize)
 		return;
 
 	nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
-	*items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
+	*items = reallocarray(*items, nmaxsize, sizeof(struct pmcpl_ct_instr));
 	if (*items == NULL)
 		errx(EX_SOFTWARE, "ERROR: out of memory");
 	bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),

Modified: head/usr.sbin/pmcstat/pmcstat_log.c
==============================================================================
--- head/usr.sbin/pmcstat/pmcstat_log.c	Sat Apr 22 14:50:11 2017	(r317283)
+++ head/usr.sbin/pmcstat/pmcstat_log.c	Sat Apr 22 16:02:40 2017	(r317284)
@@ -535,8 +535,8 @@ pmcstat_image_add_symbols(struct pmcstat
 	 * Allocate space for the new entries.
 	 */
 	firsttime = image->pi_symbols == NULL;
-	symptr = realloc(image->pi_symbols,
-	    sizeof(*symptr) * (image->pi_symcount + nfuncsyms));
+	symptr = reallocarray(image->pi_symbols,
+	    image->pi_symcount + nfuncsyms, sizeof(*symptr));
 	if (symptr == image->pi_symbols) /* realloc() failed. */
 		return;
 	image->pi_symbols = symptr;
@@ -587,8 +587,8 @@ pmcstat_image_add_symbols(struct pmcstat
 	 * Return space to the system if there were duplicates.
 	 */
 	if (newsyms < nfuncsyms)
-		image->pi_symbols = realloc(image->pi_symbols,
-		    sizeof(*symptr) * image->pi_symcount);
+		image->pi_symbols = reallocarray(image->pi_symbols,
+		    image->pi_symcount, sizeof(*symptr));
 
 	/*
 	 * Keep the list of symbols sorted.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201704221602.v3MG2eC3065978>