Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Mar 2017 04:40:07 +0000 (UTC)
From:      Cy Schubert <cy@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r315368 - in head/contrib/ipfilter: . iplang ipsd ipsend lib tools
Message-ID:  <201703160440.v2G4e7at033496@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: cy
Date: Thu Mar 16 04:40:07 2017
New Revision: 315368
URL: https://svnweb.freebsd.org/changeset/base/315368

Log:
  calloc() and realloc() modernization.
  
  This commit replaces calloc calls, which called calloc() as if it were
  malloc() by allocating a multiple of objects as a sizeof multiplied by
  the number of objects. The patch rectifies this by calling calloc() as
  it was meant to be called.
  
  This commit also replaces realloc() with reallocarray() in a similar
  fashion as above. Instead of calculating the memory to reallocated
  (changed) by multiplying sizeof by the number of objects, the sizeof
  and number are passed as separate arguments to reallocarray(), letting
  reallocarray() do the multiplication instead. Like the calloc()
  adjustment above, this is approach is cleaner and more elegant than
  than the previous code.
  
  This has been tested on my production firewall and a laptop (also
  running ipfilter).
  
  Submitted by:	pfg
  MFC after:	6 weeks

Modified:
  head/contrib/ipfilter/ip_fil.c
  head/contrib/ipfilter/iplang/iplang_l.l
  head/contrib/ipfilter/ipsd/ipsd.c
  head/contrib/ipfilter/ipsd/ipsdr.c
  head/contrib/ipfilter/ipsend/lsock.c
  head/contrib/ipfilter/ipsend/sock.c
  head/contrib/ipfilter/lib/parsefields.c
  head/contrib/ipfilter/lib/parseipfexpr.c
  head/contrib/ipfilter/radix_ipf.c
  head/contrib/ipfilter/tools/ipf_y.y
  head/contrib/ipfilter/tools/ipfcomp.c
  head/contrib/ipfilter/tools/ipfstat.c

Modified: head/contrib/ipfilter/ip_fil.c
==============================================================================
--- head/contrib/ipfilter/ip_fil.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/ip_fil.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -317,8 +317,8 @@ get_unit(name, family)
 	} else {
 		old_ifneta = ifneta;
 		nifs++;
-		ifneta = (struct ifnet **)realloc(ifneta,
-						  (nifs + 1) * sizeof(ifp));
+		ifneta = (struct ifnet **)reallocarray(ifneta, nifs + 1, 
+						  sizeof(ifp));
 		if (!ifneta) {
 			free(old_ifneta);
 			nifs = 0;

Modified: head/contrib/ipfilter/iplang/iplang_l.l
==============================================================================
--- head/contrib/ipfilter/iplang/iplang_l.l	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/iplang/iplang_l.l	Thu Mar 16 04:40:07 2017	(r315368)
@@ -195,7 +195,8 @@ void push_proto()
 	if (!prstack)
 		prstack = (int *)malloc(sizeof(int));
 	else
-		prstack = (int *)realloc((char *)prstack, numpr * sizeof(int));
+		prstack = (int *)reallocarray((char *)prstack, numpr,
+		    sizeof(int));
 	prstack[numpr - 1] = oldipproto;
 }
 

Modified: head/contrib/ipfilter/ipsd/ipsd.c
==============================================================================
--- head/contrib/ipfilter/ipsd/ipsd.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/ipsd/ipsd.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -129,7 +129,7 @@ int	detect(ip, tcp)
 			if (++ihp->sd_cnt == ihp->sd_sz)
 			{
 				ihp->sd_sz += 8;
-				sh = realloc(sh, ihp->sd_sz * sizeof(*sh));
+				sh = reallocarray(sh, ihp->sd_sz, sizeof(*sh));
 				ihp->sd_hit = sh;
 			}
 			qsort(sh, ihp->sd_cnt, sizeof(*sh), ipcmp);

Modified: head/contrib/ipfilter/ipsd/ipsdr.c
==============================================================================
--- head/contrib/ipfilter/ipsd/ipsdr.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/ipsd/ipsdr.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -140,7 +140,7 @@ int	detect(srcip, dport, date)
 			if (++ihp->sd_cnt == ihp->sd_sz)
 			{
 				ihp->sd_sz += 8;
-				sh = realloc(sh, ihp->sd_sz * sizeof(*sh));
+				sh = reallocarray(sh, ihp->sd_sz, sizeof(*sh));
 				ihp->sd_hit = sh;
 			}
 			qsort(sh, ihp->sd_cnt, sizeof(*sh), ipcmp);

Modified: head/contrib/ipfilter/ipsend/lsock.c
==============================================================================
--- head/contrib/ipfilter/ipsend/lsock.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/ipsend/lsock.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -163,7 +163,7 @@ struct	sock	*find_tcp(fd, ti)
 		return NULL;
 
 	fs = p->files;
-	o = (struct file **)calloc(1, sizeof(*o) * (fs->count + 1));
+	o = (struct file **)calloc(fs->count + 1, sizeof(*o));
 	if (KMCPY(o, fs->fd, (fs->count + 1) * sizeof(*o)) == -1)
 	    {
 		fprintf(stderr, "read(%#x,%#x,%d) - fd - failed\n",

Modified: head/contrib/ipfilter/ipsend/sock.c
==============================================================================
--- head/contrib/ipfilter/ipsend/sock.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/ipsend/sock.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -226,7 +226,7 @@ struct	tcpcb	*find_tcp(fd, ti)
 	    }
 #endif
 
-	o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
+	o = (struct file **)calloc(up->u_lastfile + 1, sizeof(*o));
 	if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
 	    {
 		fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
@@ -330,7 +330,7 @@ struct	tcpcb	*find_tcp(tfd, ti)
 	i = NULL;
 	t = NULL;
 
-	o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
+	o = (struct file **)calloc(fd->fd_lastfile + 1, sizeof(*o));
 	if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
 	    {
 		fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",

Modified: head/contrib/ipfilter/lib/parsefields.c
==============================================================================
--- head/contrib/ipfilter/lib/parsefields.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/lib/parsefields.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -31,7 +31,7 @@ wordtab_t *parsefields(table, arg)
 		if (fields == NULL) {
 			fields = malloc(2 * sizeof(*fields));
 		} else {
-			fields = realloc(fields, (num + 1) * sizeof(*fields));
+			fields = reallocarray(fields, num + 1, sizeof(*fields));
 		}
 
 		if (t == NULL) {

Modified: head/contrib/ipfilter/lib/parseipfexpr.c
==============================================================================
--- head/contrib/ipfilter/lib/parseipfexpr.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/lib/parseipfexpr.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -123,9 +123,9 @@ parseipfexpr(line, errorptr)
 		osize = asize;
 		asize += 4 + (items * e->ipoe_nbasearg * e->ipoe_argsize);
 		if (oplist == NULL)
-			oplist = calloc(1, sizeof(int) * (asize + 2));
+			oplist = calloc(asize + 2, sizeof(int));
 		else
-			oplist = realloc(oplist, sizeof(int) * (asize + 2));
+			oplist = reallocarray(oplist, asize + 2, sizeof(int));
 		if (oplist == NULL) {
 			error = "oplist alloc failed";
 			goto parseerror;

Modified: head/contrib/ipfilter/radix_ipf.c
==============================================================================
--- head/contrib/ipfilter/radix_ipf.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/radix_ipf.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -1192,7 +1192,7 @@ buildtab(void)
 		if (lines == 1)
 			tab = malloc(sizeof(*tab) * 2);
 		else
-			tab = realloc(tab, (lines + 1) * sizeof(*tab));
+			tab = reallocarray(tab, lines + 1, sizeof(*tab));
 		tab[lines - 1].host = strdup(line);
 		s = strchr(tab[lines - 1].host, '/');
 		*s++ = '\0';

Modified: head/contrib/ipfilter/tools/ipf_y.y
==============================================================================
--- head/contrib/ipfilter/tools/ipf_y.y	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/tools/ipf_y.y	Thu Mar 16 04:40:07 2017	(r315368)
@@ -2194,7 +2194,7 @@ char *phrase;
 
 			for (i = 0, s = strtok(phrase, " \r\n\t"); s != NULL;
 			     s = strtok(NULL, " \r\n\t"), i++) {
-				fb = realloc(fb, (i / 4 + 1) * sizeof(*fb));
+				fb = reallocarray(fb, i / 4 + 1, sizeof(*fb));
 				l = (u_32_t)strtol(s, NULL, 0);
 				switch (i & 3)
 				{

Modified: head/contrib/ipfilter/tools/ipfcomp.c
==============================================================================
--- head/contrib/ipfilter/tools/ipfcomp.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/tools/ipfcomp.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -965,7 +965,7 @@ void printC(dir)
 	frgroup_t *g;
 
 	if (m == NULL)
-		m = (mc_t *)calloc(1, sizeof(*m) * FRC_MAX);
+		m = (mc_t *)calloc(FRC_MAX, sizeof(*m));
 
 	for (g = groups; g != NULL; g = g->fg_next) {
 		if ((dir == 0) && ((g->fg_flags & FR_INQUE) != 0))

Modified: head/contrib/ipfilter/tools/ipfstat.c
==============================================================================
--- head/contrib/ipfilter/tools/ipfstat.c	Thu Mar 16 03:49:06 2017	(r315367)
+++ head/contrib/ipfilter/tools/ipfstat.c	Thu Mar 16 04:40:07 2017	(r315368)
@@ -1422,8 +1422,8 @@ static void topipstates(saddr, daddr, sp
 			tsentry++;
 			if (!maxtsentries || tsentry == maxtsentries) {
 				maxtsentries += STGROWSIZE;
-				tstable = realloc(tstable,
-				    maxtsentries * sizeof(statetop_t));
+				tstable = reallocarray(tstable, maxtsentries,
+				    sizeof(statetop_t));
 				if (tstable == NULL) {
 					perror("realloc");
 					exit(-1);



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